F
Francis Moreau
Hello,
I was wondering what was the alignment rule when allocating memories
by using malloc, and the spec says that the alignment should be so
that the pointer returned can be assigned to a pointer to any type of
objects.
So I thought that 8 bytes of alignment should be enough for the
purpose on a 64 bits cpu, but the following simple test program makes
me wondering if it's really true:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int i;
for (i = 0; i < 18; i++)
printf("%02d: %p\n", i, malloc(1));
return 0;
}
And the ouput is:
00: 0xca7010
01: 0xca7030
02: 0xca7050
03: 0xca7070
04: 0xca7090
05: 0xca70b0
06: 0xca70d0
07: 0xca70f0
08: 0xca7110
09: 0xca7130
10: 0xca7150
11: 0xca7170
12: 0xca7190
13: 0xca71b0
14: 0xca71d0
15: 0xca71f0
16: 0xca7210
17: 0xca7230
So every returned pointers are 32 bytes aligned which looks like a
very waste of memory for small malloced memory chunk.
Could anybody enlight me ?
Thanks
I was wondering what was the alignment rule when allocating memories
by using malloc, and the spec says that the alignment should be so
that the pointer returned can be assigned to a pointer to any type of
objects.
So I thought that 8 bytes of alignment should be enough for the
purpose on a 64 bits cpu, but the following simple test program makes
me wondering if it's really true:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int i;
for (i = 0; i < 18; i++)
printf("%02d: %p\n", i, malloc(1));
return 0;
}
And the ouput is:
00: 0xca7010
01: 0xca7030
02: 0xca7050
03: 0xca7070
04: 0xca7090
05: 0xca70b0
06: 0xca70d0
07: 0xca70f0
08: 0xca7110
09: 0xca7130
10: 0xca7150
11: 0xca7170
12: 0xca7190
13: 0xca71b0
14: 0xca71d0
15: 0xca71f0
16: 0xca7210
17: 0xca7230
So every returned pointers are 32 bytes aligned which looks like a
very waste of memory for small malloced memory chunk.
Could anybody enlight me ?
Thanks