X-Authentication-Warning: delorie.com: mailnull set sender to djgpp-workers-bounces using -f Date: Mon, 11 Feb 2002 11:48:18 -0600 From: Eric Rudd Subject: Re: Alignment problem To: djgpp-workers AT delorie DOT com Message-id: <3C6803E2.38801604@cyberoptics.com> Organization: CyberOptics MIME-version: 1.0 X-Mailer: Mozilla 4.72 [en] (Win95; U) Content-type: text/plain; charset=us-ascii Content-transfer-encoding: 7bit X-Accept-Language: en,pdf References: <3C629769 DOT AEAFB611 AT cyberoptics DOT com> <379-Fri08Feb2002101042+0200-eliz AT is DOT elta DOT co DOT il> <200202081420 DOT g18EKWb06863 AT envy DOT delorie DOT com> <7872-Fri08Feb2002203948+0200-eliz AT is DOT elta DOT co DOT il> <200202081853 DOT g18IrgO08699 AT envy DOT delorie DOT com> Reply-To: djgpp-workers AT delorie DOT com 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 #include #include 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