www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2002/02/15/01:29:15

X-Authentication-Warning: delorie.com: mailnull set sender to djgpp-bounces using -f
Message-ID: <0BA32251E589D2118EA60008C70DDCAB025F91AF@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: Structure size problem
Date: Fri, 15 Feb 2002 07:27:37 +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_01C1B5E9.DC8844D0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

It is not a bug.
In order to work efficiently, compiler aligns datas, since processor
accesses them using a 16 bit or 32 bit data bus.
So, KFType is implemented as a 16 bit word, and 8 bits of memory are =
"lost"

To modify alignment, use .align directive

Regards

-----Message d'origine-----
De : Alvin Lau [mailto:alvin_lau AT yahoo DOT com]
Envoy=E9 : vendredi 15 f=E9vrier 2002 06:28
=C0 : djgpp AT delorie DOT com
Objet : Structure size problem


Here is my program:

#include <stdio.h>

typedef unsigned char byte;
typedef unsigned long dword;

struct tagKFName2 {
  dword Index;
  byte KFType;
  char KFName[30];
};
struct tagKFName {
  byte unknown[5];
  char KFName[30];
};
typedef struct tagKFName KFNAME;
typedef struct tagKFName2 KFNAME2;
int main()
{
  KFNAME buf;
  KFNAME2 buf2;

  printf("Size KFNAME: %d\n\nSize KFNAME2:
%d\n",sizeof(KFNAME),sizeof(KFNAME2));
  printf("Index: %d\nKFType: %d\nKFName:
%d\n",sizeof(buf2.Index),sizeof(buf2.KFType),sizeof(buf2.KFName));

  return 0;
}

After running this program, I got the output as follow:

Size KFNAME: 35

Size KFNAME2: 36
Index: 4
KFType: 1
KFName: 30

Obviously there have a problem about the structure size in KFNAME2, =
this
structure has three members (Index,KFType,KFName). According the output =
of
this program, the size of Index is 4, KFType is 1, KFName is 30. Then =
4+1+30
should be 35, but the total size of KFNAME2 is 36. So, I want to ask =
some
questions:
Is this a bug in DJGPP ?
How to solve this problem ?



------_=_NextPart_001_01C1B5E9.DC8844D0
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: Structure size problem</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=3D2>It is not a bug.</FONT>
<BR><FONT SIZE=3D2>In order to work efficiently, compiler aligns datas, =
since processor accesses them using a 16 bit or 32 bit data bus.</FONT>
<BR><FONT SIZE=3D2>So, KFType is implemented as a 16 bit word, and 8 =
bits of memory are &quot;lost&quot;</FONT>
</P>

<P><FONT SIZE=3D2>To modify alignment, use .align directive</FONT>
</P>

<P><FONT SIZE=3D2>Regards</FONT>
</P>

<P><FONT SIZE=3D2>-----Message d'origine-----</FONT>
<BR><FONT SIZE=3D2>De : Alvin Lau [<A =
HREF=3D"mailto:alvin_lau AT yahoo DOT com">mailto:alvin_lau AT yahoo DOT com</A>]</FON=
T>
<BR><FONT SIZE=3D2>Envoy=E9 : vendredi 15 f=E9vrier 2002 06:28</FONT>
<BR><FONT SIZE=3D2>=C0 : djgpp AT delorie DOT com</FONT>
<BR><FONT SIZE=3D2>Objet : Structure size problem</FONT>
</P>
<BR>

<P><FONT SIZE=3D2>Here is my program:</FONT>
</P>

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

<P><FONT SIZE=3D2>typedef unsigned char byte;</FONT>
<BR><FONT SIZE=3D2>typedef unsigned long dword;</FONT>
</P>

<P><FONT SIZE=3D2>struct tagKFName2 {</FONT>
<BR><FONT SIZE=3D2>&nbsp; dword Index;</FONT>
<BR><FONT SIZE=3D2>&nbsp; byte KFType;</FONT>
<BR><FONT SIZE=3D2>&nbsp; char KFName[30];</FONT>
<BR><FONT SIZE=3D2>};</FONT>
<BR><FONT SIZE=3D2>struct tagKFName {</FONT>
<BR><FONT SIZE=3D2>&nbsp; byte unknown[5];</FONT>
<BR><FONT SIZE=3D2>&nbsp; char KFName[30];</FONT>
<BR><FONT SIZE=3D2>};</FONT>
<BR><FONT SIZE=3D2>typedef struct tagKFName KFNAME;</FONT>
<BR><FONT SIZE=3D2>typedef struct tagKFName2 KFNAME2;</FONT>
<BR><FONT SIZE=3D2>int main()</FONT>
<BR><FONT SIZE=3D2>{</FONT>
<BR><FONT SIZE=3D2>&nbsp; KFNAME buf;</FONT>
<BR><FONT SIZE=3D2>&nbsp; KFNAME2 buf2;</FONT>
</P>

<P><FONT SIZE=3D2>&nbsp; printf(&quot;Size KFNAME: %d\n\nSize =
KFNAME2:</FONT>
<BR><FONT SIZE=3D2>%d\n&quot;,sizeof(KFNAME),sizeof(KFNAME2));</FONT>
<BR><FONT SIZE=3D2>&nbsp; printf(&quot;Index: %d\nKFType: =
%d\nKFName:</FONT>
<BR><FONT =
SIZE=3D2>%d\n&quot;,sizeof(buf2.Index),sizeof(buf2.KFType),sizeof(buf2.K=
FName));</FONT>
</P>

<P><FONT SIZE=3D2>&nbsp; return 0;</FONT>
<BR><FONT SIZE=3D2>}</FONT>
</P>

<P><FONT SIZE=3D2>After running this program, I got the output as =
follow:</FONT>
</P>

<P><FONT SIZE=3D2>Size KFNAME: 35</FONT>
</P>

<P><FONT SIZE=3D2>Size KFNAME2: 36</FONT>
<BR><FONT SIZE=3D2>Index: 4</FONT>
<BR><FONT SIZE=3D2>KFType: 1</FONT>
<BR><FONT SIZE=3D2>KFName: 30</FONT>
</P>

<P><FONT SIZE=3D2>Obviously there have a problem about the structure =
size in KFNAME2, this</FONT>
<BR><FONT SIZE=3D2>structure has three members (Index,KFType,KFName). =
According the output of</FONT>
<BR><FONT SIZE=3D2>this program, the size of Index is 4, KFType is 1, =
KFName is 30. Then 4+1+30</FONT>
<BR><FONT SIZE=3D2>should be 35, but the total size of KFNAME2 is 36. =
So, I want to ask some</FONT>
<BR><FONT SIZE=3D2>questions:</FONT>
<BR><FONT SIZE=3D2>Is this a bug in DJGPP ?</FONT>
<BR><FONT SIZE=3D2>How to solve this problem ?</FONT>
</P>
<BR>

</BODY>
</HTML>
------_=_NextPart_001_01C1B5E9.DC8844D0--

- Raw text -


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