473,327 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

sizeof in old C compilers

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.
Jun 27 '08 #1
6 1154
WDS <Bi**@seurer.netwrites:
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.

--
Ben.
Jun 27 '08 #2
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(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.
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.)

--
Er*********@sun.com
Jun 27 '08 #3
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
Jun 27 '08 #4

"Ben Bacarisse" <be********@bsb.me.ukschreef in bericht
news:87************@bsb.me.uk...
WDS <Bi**@seurer.netwrites:
>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.

Jun 27 '08 #5
"Serve L" <ni@hao.comwrites:
"Ben Bacarisse" <be********@bsb.me.ukschreef in bericht
news:87************@bsb.me.uk...
>WDS <Bi**@seurer.netwrites:
>>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.
<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.
Jun 27 '08 #6
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
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

70
by: Roy Yao | last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ? According to my analysis, operator sizeof, (type) and * have the same precedence, and they combine from right to left. Then this...
2
by: dharmesh Gupta | last post by:
Hi All, the following program gives out put as 4 1 but i am unable to understand it , coulf anyone help me out Thanks Dharmesh
12
by: jl_post | last post by:
Dear C++ community, I have a question regarding the size of C++ std::strings. Basically, I compiled the following code under two different compilers: std::string someString = "Hello, world!";...
10
by: Sean | last post by:
I have a struct that I wrote to test a protocol. The idea I had was to just declare the elements of the struct in the order in which they are sent and received as defined by the protocol. ...
5
by: Chris McDonald | last post by:
I've been trying to wean myself off using parentheses after the sizeof operator (and after the return keyword, too), but my understanding is challenged by the 4th use of sizeof in the following...
5
by: Francois Grieu | last post by:
Does this reliably cause a compile-time error when int is not 4 bytes ? enum { int_size_checked = 1/(sizeof(int)==4) }; Any better way to check the value of an expression involving sizeof...
40
by: Spiros Bousbouras | last post by:
Do you have an example of an implementation where sizeof(short int) does not divide sizeof(int) or sizeof(int) does not divide sizeof(long int) or sizeof(long int) does not divide sizeof(long long...
38
by: James Brown | last post by:
All, I have a quick question regarding the size of pointer-types: I believe that the sizeof(char *) may not necessarily be the same as sizeof(int *) ? But how about multiple levels of pointers...
72
by: goacross | last post by:
char ch='a'; int v=sizeof ++ch; cout<<ch<<endl;// output: 'a' why not 'b'? thanks
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.