Mail Archives: djgpp/2002/02/08/01:25:12
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)&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>> </FONT>
<BR><FONT SIZE=3D2>> When I execute the small program</FONT>
<BR><FONT SIZE=3D2>> </FONT>
<BR><FONT SIZE=3D2>> #include <stdio.h></FONT>
<BR><FONT SIZE=3D2>> #include <stdlib.h></FONT>
<BR><FONT SIZE=3D2>> </FONT>
<BR><FONT SIZE=3D2>> int main(void) {</FONT>
<BR><FONT SIZE=3D2>> void *ptr;</FONT>
<BR><FONT SIZE=3D2>> </FONT>
<BR><FONT SIZE=3D2>> ptr =3D malloc(1024);</FONT>
<BR><FONT SIZE=3D2>> if (((int) ptr) & 7) =
{</FONT>
<BR><FONT SIZE=3D2>> =
printf(" ptr %p not 8-byte aligned.\n", ptr);</FONT>
<BR><FONT SIZE=3D2>> } else {</FONT>
<BR><FONT SIZE=3D2>> =
printf(" ptr %p 8-byte aligned.\n", =
ptr);</FONT>
<BR><FONT SIZE=3D2>> }</FONT>
<BR><FONT SIZE=3D2>> free(ptr);</FONT>
<BR><FONT SIZE=3D2>> return 0;</FONT>
<BR><FONT SIZE=3D2>> }</FONT>
<BR><FONT SIZE=3D2>> </FONT>
<BR><FONT SIZE=3D2>> I get output like</FONT>
<BR><FONT SIZE=3D2>> </FONT>
<BR><FONT SIZE=3D2>> ptr 8f4e4 not 8-byte aligned.</FONT>
<BR><FONT SIZE=3D2>> </FONT>
<BR><FONT SIZE=3D2>> about half the time. I am currently =
running gcc 2.95.3, binutils</FONT>
<BR><FONT SIZE=3D2>> 2.11.2, CWSDPMI r5 under PC-DOS 6.3. The =
problem also exists in a DOS</FONT>
<BR><FONT SIZE=3D2>> box under Win95 (where CWSDPMI is not being =
used).</FONT>
<BR><FONT SIZE=3D2>> </FONT>
<BR><FONT SIZE=3D2>> I am calling gcc with the following =
options:</FONT>
<BR><FONT SIZE=3D2>> </FONT>
<BR><FONT SIZE=3D2>> -O2</FONT>
<BR><FONT SIZE=3D2>> -march=3Dpentium</FONT>
<BR><FONT SIZE=3D2>> -Wall</FONT>
<BR><FONT SIZE=3D2>> -fomit-frame-pointer</FONT>
<BR><FONT SIZE=3D2>> </FONT>
<BR><FONT SIZE=3D2>> I thought that this alignment problem had been =
solved in gcc 2.95 and</FONT>
<BR><FONT SIZE=3D2>> binutils 2.9.1. What should I do to =
diagnose this problem further?</FONT>
</P>
<P><FONT SIZE=3D2>Is there a problem? The (int) cast is not =
necessarily valid. Try</FONT>
<BR><FONT SIZE=3D2>this modification (note alloc of 1023):</FONT>
</P>
<P><FONT SIZE=3D2>#include <stdio.h></FONT>
<BR><FONT SIZE=3D2>#include <stdlib.h></FONT>
</P>
<P><FONT SIZE=3D2>int main(void) {</FONT>
<BR><FONT SIZE=3D2> void *ptr;</FONT>
<BR><FONT SIZE=3D2> int v;</FONT>
<BR><FONT SIZE=3D2> int a, i;</FONT>
</P>
<P><FONT SIZE=3D2> for (i =3D 0; i < 8; i++) {</FONT>
<BR><FONT SIZE=3D2> ptr =3D =
malloc(1023);</FONT>
<BR><FONT SIZE=3D2> v =3D =
(int)ptr;</FONT>
<BR><FONT SIZE=3D2> =
if (!(v & 31)) a =3D 32;</FONT>
<BR><FONT SIZE=3D2> else if (!(v & 15)) a =
=3D 16;</FONT>
<BR><FONT SIZE=3D2> else if (!(v & =
7)) a =3D 8;</FONT>
<BR><FONT SIZE=3D2> else if (!(v & =
3)) a =3D 4;</FONT>
<BR><FONT SIZE=3D2> else if (!(v & =
1)) a =3D 2;</FONT>
<BR><FONT SIZE=3D2> =
else &n=
bsp; a =3D 1;</FONT>
<BR><FONT SIZE=3D2> printf(" ptr %p is =
%2d-byte aligned. (int)p =3D %d6\n", ptr, a,</FONT>
<BR><FONT SIZE=3D2>v);</FONT>
<BR><FONT SIZE=3D2> }</FONT>
<BR><FONT SIZE=3D2> 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. 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> Available for consulting/temporary =
embedded and systems.</FONT>
<BR><FONT SIZE=3D2> (Remove "XXXX" from reply =
address. yahoo works unmodified)</FONT>
<BR><FONT SIZE=3D2> <A =
HREF=3D"mailto:uce AT ftc DOT gov">mailto:uce AT ftc DOT gov</A> (for spambots =
to harvest)</FONT>
</P>
</BODY>
</HTML>
------_=_NextPart_001_01C1B069.2A872880--
- Raw text -