void *p = malloc(1000);
*((int*)p) = 666;
*((int*)p+sizeof(int)) = 777;
int i;
for (i = 0; i<10; ++i)
printf("%d ", *((int*)p+sizeof(int)*i));
Is the manual offset being resolved at compile time or does it add overhead of performing arithmetic operations during runtime?
Even if you have a constant instead of
sizeof(int), compiler cannot know in advance the address inp, so it will have to do the addition. If you have something likei = sizeof(int)+4then it should do the optimization compile time and directly setito8.Also, I think when you do:
what you mean is:
similarly
printf("%d ", *((int*)p+sizeof(int)*i));should be: