Last active
March 16, 2024 06:37
-
-
Save denisdemaisbr/8a6bceb38abcec2595fd7a072886f428 to your computer and use it in GitHub Desktop.
one simple way to align memory alloc'ed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
one simple way to align memory alloc'ed | |
based on sqlite3 macro | |
+7 = 8 (64-bit) | |
+3 = 4 (32-bit) | |
HINT: you can try ptrdiff_t too =) | |
*/ | |
#include <stdio.h> | |
#define __align(x) (((x)+7)&~7) | |
int main() { | |
for (int i=0; i<19; i++) { | |
printf("i=%d aligned=%d\n", i, __align(i)); | |
} | |
return 0; | |
} | |
/* output: | |
i=0 aligned=0 | |
i=1 aligned=8 | |
i=2 aligned=8 | |
i=3 aligned=8 | |
i=4 aligned=8 | |
i=5 aligned=8 | |
i=6 aligned=8 | |
i=7 aligned=8 | |
i=8 aligned=8 | |
i=9 aligned=16 | |
... | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment