0
    int meta_size = 24;
    node_t* prev;

    printf("%lx, ", prev + meta_size );
    printf("%lx, ", prev); 
    printf("%lx, ", meta_size);

out put: 1519240, 1519000, 18 how this is happening?

canbax
  • 3,432
  • 1
  • 27
  • 44

2 Answers2

2

Prev is a unitialised pointer, it contains (a random, because it is not initialised) memory address.

printf("%lx, ", prev + meta_size );//Prints the memory address prev is pointing to + (sizeof(node_t) * meta_size)
printf("%lx, ", prev);             //Prints the memory address prev is pointing to
printf("%lx, ", meta_size);        //Prints meta_size, 18 is 24 in hexidecimal, because of the 'x' in %lx 

However, the first 2 lines are undefined behaviour because pointers should be printed with %p

Unimportant
  • 2,076
  • 14
  • 19
0

if you do sizeof(node_t) I'm pretty sure you will obtain 10.

Dinsdale
  • 184
  • 3
  • 12
  • 1
    Actually `24` decimal or `0x18` hex. Note that all of the output numbers are hex. – user3386109 Apr 16 '16 at 19:38
  • yes you are right. When you add n to a node_t pointer, the pointer address will be increase by n * (sizeof(node_t)) thats why it is increase by 240 in base 16 = 576 in base 10 = 24 * 24 in base 10. – canbax Apr 17 '16 at 03:09