0
char        *foo1 = (char*)malloc(8 * sizeof(foo1));
char        foo2[8];
static char foo3[8];

if ((uintptr_t)fooX % sizeof(long) == 0)
    printf("Zero\n");
else
    printf("Non-zero\n");
  • Replacing fooX by foo1 prints "Zero".
  • Replacing fooX by foo2 prints "Zero".
  • Replacing fooX by foo3 prints "Non-zero".

I understand the code but I don't get why I get zero or non-zero for each of the foo's, neither when I will get zero or not (since there are other ways/keywords to declare variables). Can you explain me ?

This code is partly taken from this post about memcpy()

Community
  • 1
  • 1
Bilow
  • 2,194
  • 1
  • 19
  • 34
  • 1
    You are doing arithmetic with an address and a value. All you are getting here is if the location of the variables are aligned to longs in memory. The `static` value is not aligned apparently. BTW in C, this gives a compiler error. – Bart Friederichs Oct 11 '15 at 10:43
  • @BartFriederichs Thanks for pointing out memory alignment. Now I get it better. But this code (with appropriate includes and main declaration), does compile on my system (clang on OSX), with `-Wall -Wextra -Werror`. What error do you get ? – Bilow Jun 05 '16 at 14:52

0 Answers0