From: "DeHackEd" Newsgroups: comp.os.msdos.djgpp References: <369A62B1 DOT 5D936926 AT regiolicht DOT nl> Subject: Re: Char-in-struct bug!? Lines: 43 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.0810.800 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.0810.800 Message-ID: <8Ctm2.3743$id.207@cabot.ops.attcanada.net> Date: Mon, 11 Jan 1999 15:56:15 -0500 NNTP-Posting-Host: 142.194.222.181 X-Trace: cabot.ops.attcanada.net 916088324 142.194.222.181 (Mon, 11 Jan 1999 20:58:44 GMT) NNTP-Posting-Date: Mon, 11 Jan 1999 20:58:44 GMT To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com >I found out, that when you use a struct with djgpp (both in .c and .cpp >files) like this: > >typedef struct { > char x, y, z; > } my_type1; > >the size will be 3 bytes, but when you have one like this: Yes, it is about alignment. That's normal. If you want to correct this, read on... > >typedef struct { > char x, y, z; > int i; > } my_type2; > >the size will be 8 bytes, even if I remove y or y and z. When I add a >char behind i, the size will grow to 12 bytes! When using short ints, I >get the same problem. > >Is this some kind of dword/long int aligning? Or is it a bug? It is for alignment. The CPU can access data better if it's aligned on a specific size. Int's should be aligned on a 4 byte boundry, chars are fine the way they are. FAQ section 22.10 > >I found this strange thing when I made a structure containing a .bmp >header, which uses as the first two bytes, the chars 'B' and 'M'. >Compiling with turbo c++ 3.0 gives a (correct) structure size of 54, but >compiling with djgpp gives a size of 56 (2 zero chars are added after >the first two). > >If this is not a bug, how can I correct this? > >Henno Vermeulen. > >