Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   [gcc 4.4.3, ubuntu 10.04] "sizeof" and pointer (http://www.go4expert.com/forums/gcc-443-ubuntu-1004-sizeof-pointer-t23021/)

tukki 16Aug2010 13:34

[gcc 4.4.3, ubuntu 10.04] "sizeof" and pointer
 
I was fiddling around to find the "size of" operator

Code:

printf("%d\n",sizeof(int));
As expected the ans is 4 (in ubuntu)

Code:

printf("%d\n",sizeof(int) + (int )'\0' );
And the ans is = 4 (4 for int + 0 for null character). ok.

Code:

printf("%d\n",sizeof(int) + (int *)'\0' );
The answer is =16 (4 for int and 12 for ???).

Code:

printf("%d\n",sizeof(long double) + (int )'\0' );
Ans=12 (12 + 0)

Code:

printf("%d\n",sizeof(long double) + (int *)'\0' );
Ans=48 (12 + 36)


The general pattern i observed in such code is
Code:

printf("%d\n",sizeof(TYPE) + (int *)'\0' );
Ans=( size of(TYPE) + ( 3 * size of(TYPE))

Could someone take a look and explain this behavior.

Thanks

tukki 16Aug2010 19:58

Re: [gcc 4.4.3, ubuntu 10.04] "sizeof" and pointer
 
Hello Everyone.
The reply i got from a forum is that even i am getting a pattern it is platform dependent and so undetermined. But i dont feel that because i see the same pattern on windows platfrom with DevC and Visual Studio, with the exception of TurboC and in ubuntu the pattern is same as in windows...
Eveny time i am getting the output as
( sizeof(TYPE) + ( 3 * sizeof(TYPE) ).

Thanks

Ancient Dragon 17Aug2010 09:53

Re: [gcc 4.4.3, ubuntu 10.04] "sizeof" and pointer
 
>>printf("%d\n",sizeof(int) + (int *)'\0' );
That is an incorrect statement.
Code:

printf("%d\n",sizeof(int) + sizeof(int *));

xpi0t0s 17Aug2010 17:44

Re: [gcc 4.4.3, ubuntu 10.04] "sizeof" and pointer
 
Quote:

Originally Posted by tukki (Post 71574)
...Could someone...explain this behavior.

Undefined. You're casting a single char to a pointer to int, then implicitly casting it to an integer. The only possible answer to this is: tell us what you are trying to do, and we will tell you the correct way to do it. Whatever it is you're trying to do, this is not the correct solution, but we cannot guess what problem you're trying to solve.

xpi0t0s 17Aug2010 17:47

Re: [gcc 4.4.3, ubuntu 10.04] "sizeof" and pointer
 
What you might try if you really want to see what happens if you cast '\0' to a pointer to int is something like
Code:

int *x=(int*)'\0';
unsigned char *y=(unsigned char *)&x;
printf("%02x %02x %02x %02x\n", y[0], y[1], y[2], y[3]);

then this will display what ends up in x.

This behaviour is likely to be very compiler specific so there's not much point me testing on Visual Studio 2008.

tukki 18Aug2010 15:54

Re: [gcc 4.4.3, ubuntu 10.04] "sizeof" and pointer
 
@Ancient Dragon: Thanks for replying. I didn't understand what u pointed out. Could you elaborate.

@xpi0t0s: Thanks for replying. Its behavior is undefined with respect to different OS but otherwise predictable, if one knows the size of various types(as one wise man pointed out). What i have understood by gaining from helpful people like yourself is:

This is pointer arithmetic. as in
Code:

int i[5]={1,2,3,4,5};
int *p=i;
p=p+3;

Here after execution of the 3rd line, the pointer would have advanced 3 positions from its current position. i.e. if initially pointer was at i[0] then after this pointer would advance to i[4]. But since an int is 2 Bytes(normally), on the memory level, the pointer advances 6 bytes(3 * 2bytes(for int)), from its current position.

One important thing to note is that sizeof(int) returns unsigned integer value.
So,
Code:

(sizeof(int)+ (char *)'\0')
becomes
Code:

( 2 + (char*)0)
which is equal to (P + 2) as in the above example, resulting in (2 * 1(for char)).

Thanks

xpi0t0s 18Aug2010 21:13

Re: [gcc 4.4.3, ubuntu 10.04] "sizeof" and pointer
 
Ah yes, of course, you're effectively using pointer semantics. So it's not undefined as I first thought, because (TYPE*)'\0' is effectively talking about an array of type TYPE starting at the NULL pointer. (TYPE*)NULL + n for integer n will therefore be n*sizeof(TYPE), and this will be an address, not (strictly) an integer, because this is how arrays work in C.

So this explains my results; in Visual Studio 2008 I got 4 4 16 8 32, so sizeof(int) is 4 and sizeof(long double) is 8.

(TYPE is of course always int, in your code.)

If you like obfuscated code you should take a look at the IOCCC (google it). You'll find code there written by people with waaaaaaaaay too much time and intelligence.

tukki 25Aug2010 11:16

Re: [gcc 4.4.3, ubuntu 10.04] "sizeof" and pointer
 
@xpi0t0s: Yeah this really is a twisted code. i came across it by accident while messing with these statements.
Quote:

you should take a look at the IOCCC
Thanks for the advice.

Thanks


All times are GMT +5.5. The time now is 21:53.