align.c (1133B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main(void) 5 { 6 printf("Byte alignment of double is: %zu\n", _Alignof(double)); 7 8 printf("\nSTACK ALLOCATION:\n"); 9 _Alignas(8) double d; 10 // _Alignas(32) double d; 11 // _Alignas(64) double d; 12 printf("Address of d % 2 = %zu\n", ((size_t)&d) % 2); 13 printf("Address of d % 4 = %zu\n", ((size_t)&d) % 4); 14 printf("Address of d % 8 = %zu\n", ((size_t)&d) % 8); 15 printf("Address of d % 16 = %zu\n", ((size_t)&d) % 16); 16 printf("Address of d % 32 = %zu\n", ((size_t)&d) % 32); 17 printf("Address of d % 64 = %zu\n", ((size_t)&d) % 64); 18 19 printf("\nHEAP ALLOCATION:\n"); 20 double *pd; 21 posix_memalign((void **)&pd, 8, sizeof(double)); // segfault if alignment <8 22 printf("Address of pd % 2 = %zu\n", ((size_t)pd) % 2); 23 printf("Address of pd % 4 = %zu\n", ((size_t)pd) % 4); 24 printf("Address of pd % 8 = %zu\n", ((size_t)pd) % 8); 25 printf("Address of pd % 16 = %zu\n", ((size_t)pd) % 16); 26 printf("Address of pd % 32 = %zu\n", ((size_t)pd) % 32); 27 printf("Address of pd % 64 = %zu\n", ((size_t)pd) % 64); 28 free(pd); 29 return 0; 30 }