Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sizeof 1

Status
Not open for further replies.

Strogian

Technical User
Nov 11, 2000
36
US
Okay, I'm sort of confused about how sizeof works. Can I really use sizeof anywhere in the program (except #if statements) just like it is a function, or something? All of the sizeof's are replaced by the actual size of the object, right? The reason I'm asking, is this program:


#include <stdio.h>

void dosomething(int x);

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

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


I didn't really think this would work, because the compiler wouldn't know what the size of s is. But it did, so does that mean that &quot;sizeof s&quot; is simply replaced by whatever is inside the brackets following the s in the definition?
 
your sizeof returns the size of char pointer type, not the allocated memory under it. John Fill
1c.bmp


ivfmd@mail.md
 
It actually returns the allocated memory under it. Here's the output of several times running the program:

271
271

318
318

341
341
 
More fun with sizeof:

printf(&quot;%d&quot;,sizeof 'A');
will print 4 :)

char A='A';
printf(&quot;%d&quot;,sizeof A);
will print 1

Why? Every char in an expression is converted to int.
Same thing float -> double.
So 'A' become int before sizeof see it.

Also:
ind *b;
int c[5];
printf(&quot;%d&quot;,sizeof b);
will always print pointer size (4).
printf(&quot;%d&quot;,sizeof c);
print size of array (20).
printf(&quot;%d&quot;,sizeof &c);
print size of pointer (4).
but
printf(&quot;%p&quot;,c)
print(&quot;%p&quot;,&c);
will print the same address.

Conclusion: ofcause you can use sizeof as you wish, but as you see sizeof can be tricky.
Personaly I am trying to limit myself to use sizeof only to get size of integer, double ... to make my code more portable, but you can do whatever you want, just be careful.
Regards.






 
It will return size of array only if this is a stack array:
int a[10];
nevere for dynamicly allocated array.
May be only for this case:

int (*paiA)[3] = NULL;
paiA=malloc(sizeof(int[3] *5);
printf(&quot;%d&quot;,paiA[1]);
/* will print 12 (because the element in this array is
array of 3 integers), but */

printf(&quot;%d&quot;,paiA);
/*will always print size of the pointer (4).*/

free(paiA);

Hope I did not confused you.
 
Oh Lim, I think that sizeof 'A' returns 4 because 'A' is really a pointer to the character constant A. Kind of like how you can have a function:
char blah(char *s)
{
return *s
}

And call it with:
blah(&quot;Hi&quot;);


That is one of those things that I never REALLY understood, but I'm guessing that I would if I learned to program in some assembly language.

printf(&quot;%p&quot;,c)
print(&quot;%p&quot;,&c);
^^^^^^^^^^^^^^^
That one seems weird, though. =)


I guess what I'm really asking, is how does the compiler deal with a sizeof command? Is it safe just to use sizeof like a normal function, and assume it will work no matter what? (Yes, you did confuse me btw.. I haven't learned about malloc yet =)
 
Strogian,
No, 'A' is not a pointer to char as in you example it is a char. It is just as I said. (For reference K&R 1, on page 39).
Ok, for you question &quot;How to use it?&quot;
To make it looks better just use it like that:
A = sizeof(A); instead of A = sizeof A;
B=sizeof(int); instead of B = sizeof int;
Looks really like a function and it will work no matter what:)
About c and &c. With stack arrays int c[4];
An array name in an expression is treated by the compiler as a pointer to the first element of the array (the array address taken with the & operator). So compiler add & to c and ignore your &c, because &&c has no sence. But sizeof treats it in a different way. In case sizeof c; it takes size of a whole array, in sizeof &c only a pointer to int.
I have found that this is not very consistence, but &quot;what you gona do?&quot;

Actually malloc is not so tricky. More tricky will be a difference between stack array int c[4]; and array allocated by malloc, int *a = malloc(4 * sizeof (int));
Good luck.
 
&quot;An array name in an expression is treated by the compiler as a pointer to the first element of the array&quot;
Hmmm... So, are you saying that the array variable (the pointer to the first element of the array) isn't an actual variable when the program is run? (the compiler would basically replace c[5] with *(&ch + 5) if ch was the first element?)
 
Exactly!!!
In this case:
int c[4]; compiler will allocate 4x4=16 bytes in memory. Compiler do not allocte space to store a pointer to this memory, so when he(compiler) see you &quot;c&quot; in code:
my_func(c); he has no pointer in memory to pass in this function, so he change it to pointer to first element of array.
In case:
int *pc = malloc(4*sizeof(int));
Compiler first allocate space for pointer &quot;pc&quot; (in stack memory), then allocate in dynamic memory 16 bytes and copy pointer to this 16 bytes in &quot;pc&quot;.
So in case of call:
my_func(pc); compiler has a pointer stored in memory to pass and just pass value stored in &quot;pc&quot;.

So,
my_func(c); and my_func(&c); is the same, because &&ch has no sence and compiler just ignore your &, but
my_func(pc); and my_func(&pc); is a different, because pc is a pointer to memory where our 16 bytes allocated for array, when &pc is a pointer to a memory where pointer to this 16 bytes are stored.

Funny thing:
you are absolutly right about c[5] = *(c+5); Because of this
5[c] == c[5]; *(5+c) == *(c+5); :)
 
Status
Not open for further replies.

Similar threads

  • Locked
  • Question
sizeof 1
Replies
2
Views
36
Replies
2
Views
41
  • Locked
  • Question
Replies
6
Views
57

Part and Inventory Search

Sponsor

Back
Top