sizeof in old C compilers

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

    sizeof in old C compilers

    I ran across some old C code that was written something like this:

    int array[NUM_ELEMENTS];
    .. . .
    memset(array, -1, sizeof(int)*NUM _ELEMENTS);

    The code is filled with this idiom on memsets (and memcpys and ...).
    I am trying to figure out why they just didn't code it this way:

    memset(array, -1, sizeof(array));

    The only thing I could think of is there was a time when
    sizeof(somearra y) didn't return the whole array's size. Anyone
    remember anything like that from old, old C compilers? The oldest
    reference I can find is from a 1988 printing of The C Programming
    Language and even there sizeof works as today.

    Of course the original authors may not have understood sizeof, too, I
    suppose.
  • Ben Bacarisse

    #2
    Re: sizeof in old C compilers

    WDS <Bill@seurer.ne twrites:
    I ran across some old C code that was written something like this:
    >
    int array[NUM_ELEMENTS];
    . . .
    memset(array, -1, sizeof(int)*NUM _ELEMENTS);
    >
    The code is filled with this idiom on memsets (and memcpys and ...).
    I am trying to figure out why they just didn't code it this way:
    >
    memset(array, -1, sizeof(array));
    More likely is that the idiom fails when the array has become a
    pointer. If you move the memset code into a function, the only way to
    pass the array in is as a pointer and sizeof the pointer will not be
    what one wants. If the array is allocated rather than declared, the
    same problem presents itself. I suspect a coding standard that
    mandates the explicit NUM_ELEMENTS version after some array was
    switched from being declared to malloc'd (or from declared here, to
    being declared in a calling function) and the sizeof array code broke.
    The only thing I could think of is there was a time when
    sizeof(somearra y) didn't return the whole array's size. Anyone
    remember anything like that from old, old C compilers?
    I don't go back into C's pre-history, but even the oldest pre-ANSI
    compilers I've used get the size of an array right.

    --
    Ben.

    Comment

    • Eric Sosman

      #3
      Re: sizeof in old C compilers

      WDS wrote:
      I ran across some old C code that was written something like this:
      >
      int array[NUM_ELEMENTS];
      . . .
      memset(array, -1, sizeof(int)*NUM _ELEMENTS);
      >
      The code is filled with this idiom on memsets (and memcpys and ...).
      I am trying to figure out why they just didn't code it this way:
      >
      memset(array, -1, sizeof(array));
      >
      The only thing I could think of is there was a time when
      sizeof(somearra y) didn't return the whole array's size. Anyone
      remember anything like that from old, old C compilers? The oldest
      reference I can find is from a 1988 printing of The C Programming
      Language and even there sizeof works as today.
      >
      Of course the original authors may not have understood sizeof, too, I
      suppose.
      Impossible to tell for sure, of course, but perhaps
      they wanted to avoid the problem of FAQ 6.21:

      func(array)
      int array[NUM_ELEMENTS];
      {
      memset(array, -1, sizeof array); /* oops! */
      }

      (Antiquated function definition in honor of the great age
      of the code you've been studying.)

      --
      Eric.Sosman@sun .com

      Comment

      • Andrey Tarasevich

        #4
        Re: sizeof in old C compilers

        WDS wrote:
        I ran across some old C code that was written something like this:
        >
        int array[NUM_ELEMENTS];
        . . .
        memset(array, -1, sizeof(int)*NUM _ELEMENTS);
        >
        The code is filled with this idiom on memsets (and memcpys and ...).
        I am trying to figure out why they just didn't code it this way:
        >
        memset(array, -1, sizeof(array));
        ...
        You might find it surprising, but for the large numbers of average C
        developers the idea to use the [rather elegant] idiom of applying
        'sizeof' to the target _expression_ in 'memset', 'memcpy', 'malloc' etc.
        just doesn't pop up in their head when it is supposed to. These
        developers are often hard-wired to apply 'sizeof' to _types_ and types
        only. These are the people who'd write

        T* p = (T*) malloc(sizeof(T ))

        instead of the proper and much more elegant

        T* p = malloc(sizeof *p);

        For this very reason it is more than likely than the idea to apply
        'sizeof' to 'array' never even crossed the mind of the author of that
        code. Quite likely, the author of the code didn't even know it was
        possible. He was determined to apply the 'sizeof' to the _type_, which
        is what led him to the variant you quoted. The alternative type-base
        variant variant might look as

        memset(array, -1, sizeof(int[NUM_ELEMENTS]));

        but you don't normally expect something like this from an average
        developer :)

        --
        Best regards,
        Andrey Tarasevich

        Comment

        • Serve L

          #5
          Re: sizeof in old C compilers


          "Ben Bacarisse" <ben.usenet@bsb .me.ukschreef in bericht
          news:87hcdz6pc2 .fsf@bsb.me.uk. ..
          WDS <Bill@seurer.ne twrites:
          >
          >I ran across some old C code that was written something like this:
          >>
          >int array[NUM_ELEMENTS];
          >. . .
          >memset(array , -1, sizeof(int)*NUM _ELEMENTS);
          >>
          >The code is filled with this idiom on memsets (and memcpys and ...).
          >I am trying to figure out why they just didn't code it this way:
          >>
          >memset(array , -1, sizeof(array));
          >
          More likely is that the idiom fails when the array has become a
          pointer. If you move the memset code into a function, the only way to
          pass the array in is as a pointer and sizeof the pointer will not be
          what one wants. If the array is allocated rather than declared, the
          same problem presents itself. I suspect a coding standard that
          mandates the explicit NUM_ELEMENTS version after some array was
          switched from being declared to malloc'd (or from declared here, to
          being declared in a calling function) and the sizeof array code broke.
          I agree this might be the reason that this "technique" was used.
          But still strange that sizeof(int) was used and not sizeof(*array). Then he
          would have covered moving the line into a function where array is a pointer
          *and* changing the type of array.

          Comment

          • Ben Bacarisse

            #6
            Re: sizeof in old C compilers

            "Serve L" <ni@hao.comwrit es:
            "Ben Bacarisse" <ben.usenet@bsb .me.ukschreef in bericht
            news:87hcdz6pc2 .fsf@bsb.me.uk. ..
            >WDS <Bill@seurer.ne twrites:
            >>
            >>I ran across some old C code that was written something like this:
            >>>
            >>int array[NUM_ELEMENTS];
            >>memset(arra y, -1, sizeof(int)*NUM _ELEMENTS);
            >>>
            >>The code is filled with this idiom on memsets (and memcpys and ...).
            >>I am trying to figure out why they just didn't code it this way:
            >>>
            >>memset(arra y, -1, sizeof(array));
            >>
            >More likely is that the idiom fails when the array has become a
            >pointer.
            <snip>
            I agree this might be the reason that this "technique" was used.
            But still strange that sizeof(int) was used and not
            sizeof(*array). Then he would have covered moving the line into a
            function where array is a pointer *and* changing the type of array.
            Very true. For that, I refer you to the argument -- already presented
            here -- that many people have never learned (or have forgotten) that
            sizeof can be applied to an expression.

            Types are a boon when they are (automatically) checked and a pain when
            they are not.

            --
            Ben.

            Comment

            • Default User

              #7
              Re: sizeof in old C compilers

              Serve L wrote:

              I agree this might be the reason that this "technique" was used.
              But still strange that sizeof(int) was used and not sizeof(*array).
              Then he would have covered moving the line into a function where
              array is a pointer and changing the type of array.

              I don't find that to be strange at all. As useful as it is, I don't
              find that idiom to be common outside of this newsgroup.




              Brian

              Comment

              Working...