WebShaker said:
I'd like to know if I can be sur that
uint64_t *buffer;
buffer = (uint64_t *)malloc(1024);
will return me a pointer 64 bit aligned.
I guess that you mean that the address is an integer mul-
tiple of 8 (there isn't a "bit-alignment" for memory).
And no, the address will only be an integer multiple of 8
if an object with that alignment requirement can exist on
the system under consideration (malloc() must return me-
mory properly aligned for any kind of object possible to
allocate memory for on the system).
So the good news is that in all common cases just calling
malloc() will do just fine with respect to alignment, even
if an uint64_t may not end up on at address divisible by 8
on systems that don't require an uint64_t to be aligned that
way.
Else if, how can I do that !!!
If this is a hard requirement (i.e. the guarantees of what
malloc() does aren't sufficient and you need alignment on a
8-byte boundary even if there are no objects that require
it) then you will have to allocate up to 8 extra bytes and
make sure that 'buffer' is aligned as you need it to be,
e.g. by using
char *tmp = (char *) malloc(1031);
uint64_t *buffer = (uint64_t *) ( tmp + 8 - tmp % 8 );
(With a bit of additional effort you can cut down the number
of extra bytes to allocate to 7, or, with more work, to the
bare minimum.)
Of course, the corresponding call of free() then has to be
made for 'tmp' and not 'buffer'. And if the number of bits
in a char isn't 8 things might be even more interesting;-)
Regards, Jens