X-Authentication-Warning: delorie.com: mailnull set sender to djgpp-bounces using -f Message-ID: <0BA32251E589D2118EA60008C70DDCAB025F91A3@JNJFRISEXS1.eu.jnj.com> From: "Baribaud, Christophe [JNJFR]" To: "'djgpp AT delorie DOT com'" Subject: RE: Alignment problem Date: Fri, 8 Feb 2002 07:23:47 +0100 MIME-Version: 1.0 X-Mailer: Internet Mail Service (5.5.2655.55) Content-Type: multipart/alternative; boundary="----_=_NextPart_001_01C1B069.2A872880" Reply-To: djgpp AT delorie DOT com This message is in MIME format. Since your mail reader does not understand this format, some or all of this message may not be legible. ------_=_NextPart_001_01C1B069.2A872880 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable It seems that you consider that malloc should return an aligned = pointer. It has never been the case... If gcc's library permits this (and I don't believe so) it is not the = case for other C compilers. You can align 8-byte this way : void *ptr; void *ptral; ptr =3D (void*)malloc(1024+7); ptral =3D (void*)((((int)ptr)+7)&0xFFFFFFF8); ... use ptral ... free(ptr); ptral is 8-byte aligned and at least 1024 bytes long -----Message d'origine----- De : CBFalconer [mailto:cbfalconer AT yahoo DOT com] Envoy=E9 : jeudi 7 f=E9vrier 2002 17:18 =C0 : djgpp AT delorie DOT com Objet : Re: Alignment problem Eric Rudd wrote: >=20 > When I execute the small program >=20 > #include > #include >=20 > int main(void) { > void *ptr; >=20 > ptr =3D malloc(1024); > 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; > } >=20 > I get output like >=20 > ptr 8f4e4 not 8-byte aligned. >=20 > about half the time. I am currently running gcc 2.95.3, binutils > 2.11.2, CWSDPMI r5 under PC-DOS 6.3. The problem also exists in a = DOS > box under Win95 (where CWSDPMI is not being used). >=20 > I am calling gcc with the following options: >=20 > -O2 > -march=3Dpentium > -Wall > -fomit-frame-pointer >=20 > I thought that this alignment problem had been solved in gcc 2.95 and > binutils 2.9.1. What should I do to diagnose this problem further? Is there a problem? The (int) cast is not necessarily valid. Try this modification (note alloc of 1023): #include #include int main(void) { void *ptr; int v; int a, i; for (i =3D 0; i < 8; i++) { ptr =3D malloc(1023); v =3D (int)ptr; if (!(v & 31)) a =3D 32; else if (!(v & 15)) a =3D 16; else if (!(v & 7)) a =3D 8; else if (!(v & 3)) a =3D 4; else if (!(v & 1)) a =3D 2; else a =3D 1; printf(" ptr %p is %2d-byte aligned. (int)p =3D %d6\n", ptr, a, v); } return 0; } It looks as if the integer conversion is multiplying by 10 and adding 6. Which shouldn't matter because it is undefined anyhow.=20 At any rate the displayed result is of the form 10n+6. --=20 Chuck F (cbfalconer AT yahoo DOT com) (cbfalconer AT XXXXworldnet DOT att DOT net) Available for consulting/temporary embedded and systems. (Remove "XXXX" from reply address. yahoo works unmodified) mailto:uce AT ftc DOT gov (for spambots to harvest) ------_=_NextPart_001_01C1B069.2A872880 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable RE: Alignment problem

It seems that you consider that malloc should return = an aligned pointer. It has never been the case...
If gcc's library permits this (and I don't believe = so) it is not the case for other C compilers.

You can align 8-byte this way :

void *ptr;
void *ptral;
ptr =3D (void*)malloc(1024+7);
ptral =3D = (void*)((((int)ptr)+7)&0xFFFFFFF8);

... use ptral ...

free(ptr);

ptral is 8-byte aligned and at least 1024 bytes = long

-----Message d'origine-----
De : CBFalconer [mailto:cbfalconer AT yahoo DOT com]
Envoy=E9 : jeudi 7 f=E9vrier 2002 17:18
=C0 : djgpp AT delorie DOT com
Objet : Re: Alignment problem


Eric Rudd wrote:
>
> When I execute the small program
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main(void) {
>    void *ptr;
>
>    ptr =3D malloc(1024);
>    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;
> }
>
> I get output like
>
>  ptr 8f4e4 not 8-byte aligned.
>
> about half the time.  I am currently = running gcc 2.95.3, binutils
> 2.11.2, CWSDPMI r5 under PC-DOS 6.3.  The = problem also exists in a DOS
> box under Win95 (where CWSDPMI is not being = used).
>
> I am calling gcc with the following = options:
>
>    -O2
>    -march=3Dpentium
>    -Wall
>    -fomit-frame-pointer
>
> I thought that this alignment problem had been = solved in gcc 2.95 and
> binutils 2.9.1.  What should I do to = diagnose this problem further?

Is there a problem?  The (int) cast is not = necessarily valid.  Try
this modification (note alloc of 1023):

#include <stdio.h>
#include <stdlib.h>

int main(void) {
   void *ptr;
   int   v;
   int   a, i;

   for (i =3D 0; i < 8; i++) {
     ptr =3D = malloc(1023);
     v   =3D = (int)ptr;
     = if      (!(v & 31)) a =3D 32;
     else if (!(v & 15)) a = =3D 16;
     else if (!(v & = 7))  a =3D 8;
     else if (!(v & = 3))  a =3D 4;
     else if (!(v & = 1))  a =3D 2;
     = else           &n= bsp;    a =3D 1;
     printf(" ptr %p is = %2d-byte aligned. (int)p =3D %d6\n", ptr, a,
v);
   }
   return 0;
}

It looks as if the integer conversion is multiplying = by 10 and
adding 6.  Which shouldn't matter because it is = undefined anyhow.
At any rate the displayed result is of the form = 10n+6.

--
Chuck F (cbfalconer AT yahoo DOT com) = (cbfalconer AT XXXXworldnet DOT att DOT net)
   Available for consulting/temporary = embedded and systems.
   (Remove "XXXX" from reply = address. yahoo works unmodified)
   mailto:uce AT ftc DOT gov  (for spambots = to harvest)

------_=_NextPart_001_01C1B069.2A872880--