sizeof in old C compilers

W

WDS

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(somearray) 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.
 
B

Ben Bacarisse

WDS said:
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(somearray) 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.
 
A

Andrey Tarasevich

WDS said:
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 :)
 
S

Serve L

Ben Bacarisse said:
WDS said:
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.
 
B

Ben Bacarisse

Serve L said:
Ben Bacarisse said:
WDS said:
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.
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.
 
D

Default User

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top