I write a simple code:-
void * aa = malloc(10 * sizeof(char));
printf("%X\n", aa);
free(aa);
It always prints ???????8,
What I want to ask is:
Does malloc always return a 4*n address?
And why here the last number of the address always is 8? why not 4 or C?
my environment:
ubuntu 10.04 (32 bit)
gcc version 4.4.3
The reasons are generally rooted in alignment issues; the constraint is there to fulfill processor requirements, assist cache optimization strategies or other reasons.
As part of the core purpose of satisfying system (OS, language specification, processor) alignment requirements which would otherwise result in either faults (with varying impacts depending on the system) or inefficient behavior (having to do extra memory reads to get the same data as it straddles a memory block boundary) at runtime, a minimum alignment requirement will apply.
However you may empirically find that they are aligned to an even higher degree than that strictly required by the preceding constraints. For example, a specific allocator implementation might increase it for reasons such as: