Four or Two Bytes?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hahnemann

    Four or Two Bytes?

    Does anybody know the answer to the following? An unsigned short is 2
    bytes long. Why is the following file created as 4 bytes instead of
    2?

    file = fopen("data.bin ", "wb");
    if (file != NULL)
    {
    unsigned short s = 65535;
    printf("Size of unsigned short: %d bytes\n", sizeof(unsigned
    short)); // 2 bytes - OK
    fwrite(&s, sizeof(unsigned short), sizeof(s), file);
    fclose(file); // File written as 4 bytes... hmm
    }

    - Hahnemann

  • Chris McDonald

    #2
    Re: Four or Two Bytes?

    Hahnemann <hahnemann.orti z@gmail.comwrit es:
    >Does anybody know the answer to the following? An unsigned short is 2
    >bytes long. Why is the following file created as 4 bytes instead of
    >2?
    >file = fopen("data.bin ", "wb");
    >if (file != NULL)
    >{
    unsigned short s = 65535;
    printf("Size of unsigned short: %d bytes\n", sizeof(unsigned
    >short)); // 2 bytes - OK
    fwrite(&s, sizeof(unsigned short), sizeof(s), file);
    fclose(file); // File written as 4 bytes... hmm
    >}

    Because you're asking it to?
    Check the 3rd parameter of fwrite().

    --
    Chris.

    Comment

    • Hahnemann

      #3
      Re: Four or Two Bytes?

      On May 29, 9:39 pm, Chris McDonald <ch...@csse.uwa .edu.auwrote:
      Hahnemann <hahnemann.or.. .@gmail.comwrit es:
      Does anybody know the answer to the following? An unsigned short is 2
      bytes long.  Why is the following file created as 4 bytes instead of
      2?
      file = fopen("data.bin ", "wb");
      if (file != NULL)
      {
         unsigned short s = 65535;
         printf("Size of unsigned short: %d bytes\n", sizeof(unsigned
      short)); // 2 bytes - OK
         fwrite(&s, sizeof(unsigned short), sizeof(s), file);
         fclose(file); // File written as 4 bytes... hmm
      }
      >
      Because you're asking it to?
      Check the 3rd parameter of fwrite().
      >
      --
      Chris.
      But notice that:

      printf("Size of s: %d bytes\n", sizeof(s)) ;

      returns 2 bytes. How can I make the file 2 bytes using:

      unsigned short x = 65535;

      Is this even possible?

      Comment

      • Bill Leary

        #4
        Re: Four or Two Bytes?

        "Hahnemann" <hahnemann.orti z@gmail.comwrot e in message
        news:2fcbefe8-fa8c-4c97-8cba-b28a6ecb261e@d1 g2000hsg.google groups.com...
        Does anybody know the answer to the following? An unsigned short is 2
        bytes long. Why is the following file created as 4 bytes instead of
        2?
        >
        file = fopen("data.bin ", "wb");
        if (file != NULL)
        {
        unsigned short s = 65535;
        printf("Size of unsigned short: %d bytes\n", sizeof(unsigned
        short)); // 2 bytes - OK
        fwrite(&s, sizeof(unsigned short), sizeof(s), file);
        fwrite(
        pointer to buffer ... ok
        size of an element of that buffer ... 2
        number of elements to write ... 2
        handle for file write should occur to ... ok

        So, 2 elements of 2 bytes each should write four bytes.
        fclose(file); // File written as 4 bytes... hmm
        So, fwrite worked correctly. More or less, since it wrote out 4 bytes from
        a 2 byte object. It could just about as easily have crashed when it went
        beyond the bounds of the object.
        }
        - Bill

        Comment

        • Peter Nilsson

          #5
          Re: Four or Two Bytes?

          Hahnemann <hahnemann.or.. .@gmail.comwrot e:
          Chris McDonald <ch...@csse.uwa .edu.auwrote:
          Hahnemann <hahnemann.or.. .@gmail.comwrit es:
          Does anybody know the answer to the following?
          An unsigned short is 2 bytes long.
          You mean _if_?
          Why is the following file created as 4 bytes
          instead of 2?
          Because 2 lots of 2 bytes is 4 bytes.
             unsigned short s = 65535;
             fwrite(&s, sizeof(unsigned short), sizeof(s), file);
          >
          Because you're asking it to?
          Check the 3rd parameter of fwrite().
          >
          But notice that:
          >
          printf("Size of s: %d bytes\n", sizeof(s)) ;
          >
          returns 2 bytes.  How can I make the file 2 bytes
          using:
          By only outputting 2 bytes once, instead of twice.
          unsigned short x = 65535;
          >
          Is this even possible?
          Hangliding is a topic best researched _before_ you
          through yourself off the cliff. ;)

          --
          Peter

          Comment

          • Keith Thompson

            #6
            Re: Four or Two Bytes?

            Hahnemann <hahnemann.orti z@gmail.comwrit es:
            On May 29, 9:39 pm, Chris McDonald <ch...@csse.uwa .edu.auwrote:
            >Hahnemann <hahnemann.or.. .@gmail.comwrit es:
            >Does anybody know the answer to the following? An unsigned short is 2
            >bytes long.  Why is the following file created as 4 bytes instead of
            >2?
            >file = fopen("data.bin ", "wb");
            >if (file != NULL)
            >{
               unsigned short s = 65535;
               printf("Size of unsigned short: %d bytes\n", sizeof(unsigned
            >short)); // 2 bytes - OK
               fwrite(&s, sizeof(unsigned short), sizeof(s), file);
               fclose(file); // File written as 4 bytes... hmm
            >}
            >>
            >Because you're asking it to?
            >Check the 3rd parameter of fwrite().
            >
            But notice that:
            >
            printf("Size of s: %d bytes\n", sizeof(s)) ;
            >
            returns 2 bytes.
            Yes, but ...

            "%d" expects an argument of type int; you're giving it an argument of
            type size_t. You should cast the value to int (one of the few cases
            where a cast is appropriate):

            printf("Size of s: %d bytes\n", (int)sizeof s);

            And though unsigned short is 2 bytes on your implementation, that's
            not guaranteed; it could be different on other implementations .
            How can I make the file 2 bytes using:
            >
            unsigned short x = 65535;
            >
            Is this even possible?
            Certainly.

            fwrite() writes a specified number of elements. In your case, the
            element(s) are of type unsigned short. The second argument is the
            size of each element (you're passing ``sizeof(unsign ed short)'', which
            is ok). The third argument is the number of elements (you're passing
            ``sizeof(s)''). How many elements do you want to write (hint: 1).

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
            Nokia
            "We must do something. This is something. Therefore, we must do this."
            -- Antony Jay and Jonathan Lynn, "Yes Minister"

            Comment

            • Martin

              #7
              Re: Four or Two Bytes?

              On Fri, 30 May 2008 04:59:22 +0100, Keith Thompson <kst-u@mib.orgwrote:
              "%d" expects an argument of type int; you're giving it an argument of
              type size_t. You should cast the value to int (one of the few cases
              where a cast is appropriate):
              >
              printf("Size of s: %d bytes\n", (int)sizeof s);
              Generally, isn't it better to use

              printf("Size of s: %lu bytes\n", (unsigned long)sizeof s);

              as recommended in the answer to FAQ question 7.15?

              --
              Martin

              Comment

              • Joachim Schmitz

                #8
                Re: Four or Two Bytes?

                Martin wrote:
                On Fri, 30 May 2008 04:59:22 +0100, Keith Thompson <kst-u@mib.org>
                wrote:
                >"%d" expects an argument of type int; you're giving it an argument of
                >type size_t. You should cast the value to int (one of the few cases
                >where a cast is appropriate):
                >>
                > printf("Size of s: %d bytes\n", (int)sizeof s);
                >
                Generally, isn't it better to use
                >
                printf("Size of s: %lu bytes\n", (unsigned long)sizeof s);
                >
                as recommended in the answer to FAQ question 7.15?
                Guess Keith's point is that you should cast it to whatever the format
                specified expects.

                Bye, Jojo


                Comment

                • Martin

                  #9
                  Re: Four or Two Bytes?

                  On Fri, 30 May 2008 10:19:31 +0100, Joachim Schmitz
                  <nospam.jojo@sc hmitz-digital.dewrote :
                  Guess Keith's point is that you should cast it to whatever the format
                  specified expects.
                  I'll wait for Keith's response, but I would say that sometimes it may be
                  better to change the format specified. Casting an unsigned type to signed
                  type of equal or less width has the potential for data loss.

                  --
                  Martin

                  Comment

                  • Joachim Schmitz

                    #10
                    Re: Four or Two Bytes?

                    Martin wrote:
                    On Fri, 30 May 2008 10:19:31 +0100, Joachim Schmitz
                    <nospam.jojo@sc hmitz-digital.dewrote :
                    >Guess Keith's point is that you should cast it to whatever the format
                    >specified expects.
                    >
                    I'll wait for Keith's response, but I would say that sometimes it may
                    be better to change the format specified. Casting an unsigned type to
                    signed type of equal or less width has the potential for data loss.
                    Yes it might have, but how likely is it that a data type is really larger
                    than INT_MAX?


                    Comment

                    • CBFalconer

                      #11
                      Re: Four or Two Bytes?

                      Joachim Schmitz wrote:
                      Martin wrote:
                      >Joachim Schmitz <nospam.jojo@sc hmitz-digital.dewrote :
                      >>
                      >>Guess Keith's point is that you should cast it to whatever the
                      >>format specified expects.
                      >>
                      >I'll wait for Keith's response, but I would say that sometimes
                      >it may be better to change the format specified. Casting an
                      >unsigned type to signed type of equal or less width has the
                      >potential for data loss.
                      >
                      Yes it might have, but how likely is it that a data type is
                      really larger than INT_MAX?
                      If you simply examine limits.h you will probably find that UINT_MAX
                      is roughly twice the size of INT_MAX. To me, this indicates your
                      probability is roughly 2:1. Anything over 0 is bad.

                      --
                      [mail]: Chuck F (cbfalconer at maineline dot net)
                      [page]: <http://cbfalconer.home .att.net>
                      Try the download section.

                      ** Posted from http://www.teranews.com **

                      Comment

                      • Joachim Schmitz

                        #12
                        Re: Four or Two Bytes?

                        CBFalconer wrote:
                        Joachim Schmitz wrote:
                        >Martin wrote:
                        >>Joachim Schmitz <nospam.jojo@sc hmitz-digital.dewrote :
                        >>>
                        >>>Guess Keith's point is that you should cast it to whatever the
                        >>>format specified expects.
                        >>>
                        >>I'll wait for Keith's response, but I would say that sometimes
                        >>it may be better to change the format specified. Casting an
                        >>unsigned type to signed type of equal or less width has the
                        >>potential for data loss.
                        >>
                        >Yes it might have, but how likely is it that a data type is
                        >really larger than INT_MAX?
                        >
                        If you simply examine limits.h you will probably find that UINT_MAX
                        is roughly twice the size of INT_MAX. To me, this indicates your
                        probability is roughly 2:1. Anything over 0 is bad.
                        So what? The basic daty types have sizes of 1 to 8 bytes. Sensibel structs
                        and arrays are most probaly well smaller than 32K, which is the minimun for
                        INT_MAX, so again: how big is the chance that a real life data type is
                        larger than that?

                        Bye, Jojo


                        Comment

                        • Joachim Schmitz

                          #13
                          Re: Four or Two Bytes?

                          Joachim Schmitz wrote:
                          CBFalconer wrote:
                          >Joachim Schmitz wrote:
                          >>Martin wrote:
                          >>>Joachim Schmitz <nospam.jojo@sc hmitz-digital.dewrote :
                          >>>>
                          >>>>Guess Keith's point is that you should cast it to whatever the
                          >>>>format specified expects.
                          >>>>
                          >>>I'll wait for Keith's response, but I would say that sometimes
                          >>>it may be better to change the format specified. Casting an
                          >>>unsigned type to signed type of equal or less width has the
                          >>>potential for data loss.
                          >>>
                          >>Yes it might have, but how likely is it that a data type is
                          >>really larger than INT_MAX?
                          >>
                          >If you simply examine limits.h you will probably find that UINT_MAX
                          >is roughly twice the size of INT_MAX. To me, this indicates your
                          >probability is roughly 2:1. Anything over 0 is bad.
                          So what? The basic daty types have sizes of 1 to 8 bytes. Sensibel
                          structs and arrays are most probaly well smaller than 32K, which is
                          the minimun for INT_MAX, so again: how big is the chance that a real
                          life data type is larger than that?
                          On top of that: here it is about 2 or 4, which even on the DS9K would easily
                          fit into INT_MAX.

                          Bye, Jojo


                          Comment

                          • pete

                            #14
                            Re: Four or Two Bytes?

                            Joachim Schmitz wrote:
                            On top of that: here it is about 2 or 4, which even on the DS9K would easily
                            fit into INT_MAX.
                            Not on the DS9K.
                            unsigned short can have INT_MAX padding bytes on the DS9K.

                            --
                            pete

                            Comment

                            • Joachim Schmitz

                              #15
                              Re: Four or Two Bytes?

                              pete wrote:
                              Joachim Schmitz wrote:
                              >On top of that: here it is about 2 or 4, which even on the DS9K
                              >would easily fit into INT_MAX.
                              >
                              Not on the DS9K.
                              unsigned short can have INT_MAX padding bytes on the DS9K.
                              Chapter and verse please


                              Comment

                              Working...