1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

[gcc 4.4.3, ubuntu 10.04] "sizeof" and pointer

Discussion in 'C' started by tukki, Aug 16, 2010.

  1. tukki

    tukki New Member

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

    tukki New Member

    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
     
  3. Ancient Dragon

    Ancient Dragon New Member

    >>printf("%d\n",sizeof(int) + (int *)'\0' );
    That is an incorrect statement.
    Code:
    printf("%d\n",sizeof(int) + sizeof(int *));
     
  4. xpi0t0s

    xpi0t0s Mentor

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

    xpi0t0s Mentor

    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.
     
    shabbir likes this.
  6. tukki

    tukki New Member

    @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
     
    shabbir likes this.
  7. xpi0t0s

    xpi0t0s Mentor

    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.
     
    shabbir likes this.
  8. tukki

    tukki New Member

    @xpi0t0s: Yeah this really is a twisted code. i came across it by accident while messing with these statements.
    Thanks for the advice.

    Thanks
     

Share This Page