![]() |
[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));Code:
printf("%d\n",sizeof(int) + (int )'\0' );Code:
printf("%d\n",sizeof(int) + (int *)'\0' );Code:
printf("%d\n",sizeof(long double) + (int )'\0' );Code:
printf("%d\n",sizeof(long double) + (int *)'\0' );The general pattern i observed in such code is Code:
printf("%d\n",sizeof(TYPE) + (int *)'\0' );Could someone take a look and explain this behavior. Thanks |
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 |
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 *)); |
Re: [gcc 4.4.3, ubuntu 10.04] "sizeof" and pointer
Quote:
|
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';This behaviour is likely to be very compiler specific so there's not much point me testing on Visual Studio 2008. |
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};One important thing to note is that sizeof(int) returns unsigned integer value. So, Code:
(sizeof(int)+ (char *)'\0')Code:
( 2 + (char*)0)Thanks |
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. |
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:
Thanks |
| All times are GMT +5.5. The time now is 06:50. |