Mail Archives: djgpp-workers/2002/02/11/13:06:49
DJ Delorie wrote:
>> But isn't the pointer returned to the application is 4 bytes after the
>> address returned by sbrk? If so, how can it be aligned?
>
> It doesn't align to 8 bytes. It rounds the *size* up to 8 bytes.
From experimentation with the following program,
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void) {
void *ptr;
printf("sbrk(0) = %p\n", sbrk(0));
ptr = malloc(1024);
printf("sbrk(0) = %p\n", sbrk(0));
if (((int) ptr) & 7) {
printf(" ptr %p not 8-byte aligned.\n", ptr);
} else {
printf(" ptr %p 8-byte aligned.\n", ptr);
}
free(ptr);
return 0;
}
it appears as the alignment of malloc() follows the alignment of sbrk().
That is, if malloc() gets an 8-byte aligned pointer from sbrk, malloc() also
returns an 8-byte aligned pointer, otherwise not. Perhaps malloc() assumes
that sbrk() pointers are 8-byte aligned.
Is sbrk() supposed to return aligned pointers? If not, then malloc needs to
deal with the misalignment.
-Eric
- Raw text -