473,789 Members | 2,161 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(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.
Jun 27 '08 #1
6 1175
WDS <Bi**@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.
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(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.)

--
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.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.

Jun 27 '08 #5
"Serve L" <ni@hao.comwrit es:
"Ben Bacarisse" <be********@bsb .me.ukschreef in bericht
news:87******** ****@bsb.me.uk. ..
>WDS <Bi**@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.
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
8914
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 expression should equal to "sizeof( (int)(*p) )", but the compiler does NOT think so. Why? Can anyone help me? Thanks. Best regards. Roy
2
397
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
13592
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!"; int size1 = sizeof(std::string); int size2 = sizeof(someString); and printed out the values of size1 and size2. size1 and size2 always
10
2434
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. However, writing this struct to a file produces unexpected results. Here is a test struct I wrote: struct Tester { unsigned short first; unsigned int second;
5
6200
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 code: #include <stdio.h> struct { int i; char *p; } var;
5
2900
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 before runtime ? I also have: { void check_foo_size(void);
40
3663
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 int) ? Same question for the corresponding unsigned types.
38
2609
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 to the same type? Would sizeof(char **) be the same as sizeof(char *)? And if it is, would the internal representation be the same in both cases? background on this: I'm writing a simple IDL compiler that produces 'C'
72
3050
by: goacross | last post by:
char ch='a'; int v=sizeof ++ch; cout<<ch<<endl;// output: 'a' why not 'b'? thanks
0
9659
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10400
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10134
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9977
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9011
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6754
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5413
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3692
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2903
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.