delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2002/02/11/13:06:49

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 <rudd AT cyberoptics DOT com>
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)
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 <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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019