delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2002/02/08/01:25:12

X-Authentication-Warning: delorie.com: mailnull set sender to djgpp-bounces using -f
Message-ID: <0BA32251E589D2118EA60008C70DDCAB025F91A3@JNJFRISEXS1.eu.jnj.com>
From: "Baribaud, Christophe [JNJFR]" <CBARIBAU AT jnjfr DOT JNJ DOT com>
To: "'djgpp AT delorie DOT com'" <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)
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 <stdio.h>
> #include <stdlib.h>
>=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 <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                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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3Diso-8859-1">
<META NAME=3D"Generator" CONTENT=3D"MS Exchange Server version =
5.5.2654.45">
<TITLE>RE: Alignment problem</TITLE>
</HEAD>
<BODY>

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

<P><FONT SIZE=3D2>You can align 8-byte this way :</FONT>
</P>

<P><FONT SIZE=3D2>void *ptr;</FONT>
<BR><FONT SIZE=3D2>void *ptral;</FONT>
<BR><FONT SIZE=3D2>ptr =3D (void*)malloc(1024+7);</FONT>
<BR><FONT SIZE=3D2>ptral =3D =
(void*)((((int)ptr)+7)&amp;0xFFFFFFF8);</FONT>
</P>

<P><FONT SIZE=3D2>... use ptral ...</FONT>
</P>

<P><FONT SIZE=3D2>free(ptr);</FONT>
</P>

<P><FONT SIZE=3D2>ptral is 8-byte aligned and at least 1024 bytes =
long</FONT>
</P>

<P><FONT SIZE=3D2>-----Message d'origine-----</FONT>
<BR><FONT SIZE=3D2>De : CBFalconer [<A =
HREF=3D"mailto:cbfalconer AT yahoo DOT com">mailto:cbfalconer AT yahoo DOT com</A>]</F=
ONT>
<BR><FONT SIZE=3D2>Envoy=E9 : jeudi 7 f=E9vrier 2002 17:18</FONT>
<BR><FONT SIZE=3D2>=C0 : djgpp AT delorie DOT com</FONT>
<BR><FONT SIZE=3D2>Objet : Re: Alignment problem</FONT>
</P>
<BR>

<P><FONT SIZE=3D2>Eric Rudd wrote:</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; When I execute the small program</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; #include &lt;stdio.h&gt;</FONT>
<BR><FONT SIZE=3D2>&gt; #include &lt;stdlib.h&gt;</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; int main(void) {</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp; void *ptr;</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp; ptr =3D malloc(1024);</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp; if (((int) ptr) &amp; 7) =
{</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
printf(&quot; ptr %p not 8-byte aligned.\n&quot;, ptr);</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp; } else {</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
printf(&quot; ptr %p&nbsp;&nbsp;&nbsp;&nbsp; 8-byte aligned.\n&quot;, =
ptr);</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp; }</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp; free(ptr);</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp; return 0;</FONT>
<BR><FONT SIZE=3D2>&gt; }</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; I get output like</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp; ptr 8f4e4 not 8-byte aligned.</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; about half the time.&nbsp; I am currently =
running gcc 2.95.3, binutils</FONT>
<BR><FONT SIZE=3D2>&gt; 2.11.2, CWSDPMI r5 under PC-DOS 6.3.&nbsp; The =
problem also exists in a DOS</FONT>
<BR><FONT SIZE=3D2>&gt; box under Win95 (where CWSDPMI is not being =
used).</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; I am calling gcc with the following =
options:</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp; -O2</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp; -march=3Dpentium</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp; -Wall</FONT>
<BR><FONT SIZE=3D2>&gt;&nbsp;&nbsp;&nbsp; -fomit-frame-pointer</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; I thought that this alignment problem had been =
solved in gcc 2.95 and</FONT>
<BR><FONT SIZE=3D2>&gt; binutils 2.9.1.&nbsp; What should I do to =
diagnose this problem further?</FONT>
</P>

<P><FONT SIZE=3D2>Is there a problem?&nbsp; The (int) cast is not =
necessarily valid.&nbsp; Try</FONT>
<BR><FONT SIZE=3D2>this modification (note alloc of 1023):</FONT>
</P>

<P><FONT SIZE=3D2>#include &lt;stdio.h&gt;</FONT>
<BR><FONT SIZE=3D2>#include &lt;stdlib.h&gt;</FONT>
</P>

<P><FONT SIZE=3D2>int main(void) {</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp; void *ptr;</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp; int&nbsp;&nbsp; v;</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp; int&nbsp;&nbsp; a, i;</FONT>
</P>

<P><FONT SIZE=3D2>&nbsp;&nbsp; for (i =3D 0; i &lt; 8; i++) {</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp; ptr =3D =
malloc(1023);</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp; v&nbsp;&nbsp; =3D =
(int)ptr;</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp; =
if&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (!(v &amp; 31)) a =3D 32;</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp; else if (!(v &amp; 15)) a =
=3D 16;</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp; else if (!(v &amp; =
7))&nbsp; a =3D 8;</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp; else if (!(v &amp; =
3))&nbsp; a =3D 4;</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp; else if (!(v &amp; =
1))&nbsp; a =3D 2;</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp; =
else&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp; a =3D 1;</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot; ptr %p is =
%2d-byte aligned. (int)p =3D %d6\n&quot;, ptr, a,</FONT>
<BR><FONT SIZE=3D2>v);</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp; }</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp; return 0;</FONT>
<BR><FONT SIZE=3D2>}</FONT>
</P>

<P><FONT SIZE=3D2>It looks as if the integer conversion is multiplying =
by 10 and</FONT>
<BR><FONT SIZE=3D2>adding 6.&nbsp; Which shouldn't matter because it is =
undefined anyhow. </FONT>
<BR><FONT SIZE=3D2>At any rate the displayed result is of the form =
10n+6.</FONT>
</P>

<P><FONT SIZE=3D2>-- </FONT>
<BR><FONT SIZE=3D2>Chuck F (cbfalconer AT yahoo DOT com) =
(cbfalconer AT XXXXworldnet DOT att DOT net)</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp; Available for consulting/temporary =
embedded and systems.</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp; (Remove &quot;XXXX&quot; from reply =
address. yahoo works unmodified)</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp; <A =
HREF=3D"mailto:uce AT ftc DOT gov">mailto:uce AT ftc DOT gov</A>&nbsp; (for spambots =
to harvest)</FONT>
</P>

</BODY>
</HTML>
------_=_NextPart_001_01C1B069.2A872880--

- Raw text -


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