Click to See Complete Forum and Search --> : sizeof operator (in C)


Strogian
07-17-2001, 06:35 PM
Is it legal to use the sizeof operator anywhere in a C program, except in #if statements? This book says that sizeof is a compile-time operator, so would that mean I couldn't use it if the compiler can't know the size of that object? For example:

#include <stdio.h>

void dosomething(int);

int main()
{
srand();
dosomething(rand());
return 0;
}

void dosomething(int x)
{
char s[x];
printf("%d\n", sizeof s);
}


I tried doing something like this, but ended up with a Segmentation fault. I wasn't sure why I got one, so I just decided to ask the question here. Thanks in advance.

Zoma
07-17-2001, 06:45 PM
Yeah, sizeof can be thought of of working like a #define statement. That is, all of the calls to "sizeof" are evaluated when you compile, and are, more-or-less, replaced with the actual numbers. The reason people use sizeof is to make sure that they are using the right size for a type, and this especially applies when dealing with how portable code will be.

The code you posted is bad because of:

"char s[x];"

I wouldn't think that would even compile, since an array must be defined with a number that is known at compile-time, and in your program, x is most definitely not.

Also, even if you had something like: "s[80]", sizeof(s) would still be 4 on most systems, since s is really a pointer to a character, which is probably a 32-bit pointer on your system.

Strogian
07-17-2001, 07:51 PM
I wouldn't think that would even compile, since an array must be defined with a number that is known at compile-time...

Wow, really? I never realized that!


Also, even if you had something like: "s[80]", sizeof(s) would still be 4 on most systems, since s is really a pointer to a character, which is probably a 32-bit pointer on your system.

Are you sure about this? This book I'm using (The C programming language, 2nd ed.) says that you can use sizeof to find the amount of elements in this array keytab[]:

struct key {
char *word;
int count;
} keytab[] = {
"auto", 0,
"break", 0
};

#define NKEYS (sizeof keytab / sizeof(struct key))

Strogian
07-18-2001, 11:42 AM
Oh boy...

#include <stdio.h>

void dosomething(int x);

int main()
{
int x;
srand((unsigned int) time());
do
x = rand();
while(x > 500);
dosomething(x);
printf("%d\n", x);
return 0;
}

void dosomething(int x)
{
char s[x];
printf("%d\n", sizeof s);
}

That program there works perfectly. It looks like the segmentation faults were simply caused by trying to declare an array with too many elements. Anyway, I'm definitely getting confused now. http://www.sharkyforums.com/ubb/smile.gif

Strogian
07-18-2001, 12:02 PM
Well, reading over some other message boards, it looks like there is a new standard for C called C99, that will allow variable-length arrays. (which is why char s[x] worked, on my compiler) Maybe the compiler just knew that the size of s would always be x, so it just replaced "sizeof s" with "x"?

reklis
07-18-2001, 12:27 PM
I think you're confusing what sizeof s with the number of elements in the array. s is a pointer, and so sizeof is going to return the size of that pointer. Allow me to explain:

int w;
int *x;
int y[5];
struct z { int a,b,c; };
struct z zs;
struct z *zp;

sizeof w; // the size of an int
sizeof(int); // the size of an int
sizeof x; // the size of a pointer
sizeof *x; // the size of an int
sizeof y; // the size of the array
sizeof (struct z); // the size of the structure
sizeof zs; // the size of the structure
sizeof zp; // the size of a pointer

Here's an example of using sizeof to compute array elements:

sel = 0;
while (sel != (sizeof menu / sizeof(Menu))) {
for (i = 0; i < Selections; ++i) {
// something with menu[i]
}
}

Does that make it a bit clearer?

------------------
Advocate of the Sharky (Ultra) High-Resolution Club [SHRC (http://www.sharkyforums.com/ubb/Forum17/HTML/005121.html)]
main(i){putchar(341513875>>(i-1)*5&31|!!(i<6)<<6)&&main(++i);}

Strogian
07-18-2001, 01:53 PM
sizeof y; // the size of the array
That is the same as my sizeof s;
Why does everyone want to tell me that I'm getting the size of a pointer? =) (another msg board I posted this to got a response telling me that sizeof s would be the size of a pointer) The output looks like this, btw (from running it several times):
421
421

312
312

316
316

dighn
07-18-2001, 06:56 PM
Originally posted by Strogian:
sizeof y; // the size of the array
That is the same as my sizeof s;
Why does everyone want to tell me that I'm getting the size of a pointer? =) (another msg board I posted this to got a response telling me that sizeof s would be the size of a pointer) The output looks like this, btw (from running it several times):
421
421

312
312

316
316

It really depends on how much the compiler knows
for example

char blah[50];
sizeof blah would give you 50
BUT
if you do something like blah* = malloc(50);
sizeof blah would give you the size of the pointer which is usually 4 bytes

the first blah and the second blah are both of type char*, but they give u different results because the compilers knows more in the first case...

------------------
So easy to use, no wonder it's number one!!!
Where do you want to go today?
Member of NT Jocks (http://yoconsoft.hypermart.net/images/ntjockdighn.gif)
Tesla coils are not weapons

[This message has been edited by dighn (edited July 18, 2001).]

Zoma
07-19-2001, 12:00 PM
Yeah, I was wrong, sizeof(someArray) DOES return the size of the array, and not of a pointer. I must have done something weird on my compiler http://www.sharkyforums.com/ubb/smile.gif