www.delorie.com/djgpp/bugs/show.cgi   search  
Bug 000130

When Created: 01/16/1997 12:57:48
Against DJGPP version: 2.01
By whom: leochan@gpu.srv.ualberta.ca
Abstract: struct allocated size to int incorrect
It seems that integers are sometimes given 6 bytes instead of the usual 4.
Consider the following code:

#include <dpmi.h>
#include <stdio.h>

typedef struct
{
	char VbeSignature[4];
	unsigned short VbeVersion;
	__dpmi_raddr OemStringPtr;
	char Capabilities[4];
	__dpmi_raddr VideoModePtr;
	unsigned short TotalMemory;
	unsigned short OemSoftwareRev;
	__dpmi_raddr OemVendorNamePtr;
	__dpmi_raddr OemProductNamePtr;
	__dpmi_raddr OemProductRevPtr;
	char Reserved[222];
	char OemData[256];
} VbeInfoBlock_t;

typedef struct
{
	char VbeSignature[4];
	unsigned short VbeVersion;
	__dpmi_raddr OemStringPtr;
	int Capabilities;
	__dpmi_raddr VideoModePtr;
	unsigned short TotalMemory;
	unsigned short OemSoftwareRev;
	__dpmi_raddr OemVendorNamePtr;
	__dpmi_raddr OemProductNamePtr;
	__dpmi_raddr OemProductRevPtr;
	char Reserved[222];
	char OemData[256];
} VbeInfoBlock2_t;

void main()
{
	printf("Size of structure 1: %d\n", sizeof(VbeInfoBlock_t));
        printf("Size of structure 2: %d\n", sizeof(VbeInfoBlock2_t));
}

It's output is the following:

Size of structure 1: 512
Size of structure 2: 516

The only difference between the two structures is the Capabilities field;
in the first one its defined as char[4] whereas in the second on its an int.

Workaround added: 01/16/1997 12:58:49
By whom: leochan@gpu.srv.ualberta.ca
As shown in the bug report, use char[4] instead of int and typecast if you 
need it as an int

Note added: 01/21/1997 09:05:04
By whom: terra@diku.dk (Morten Welinder)
This is not a bug but just how C works.  If we have types t1 and t2
and define "typedef struct { t1 v1; t2 v2; } t12;" then there is _NO_
guarantee that sizeof (t12) = sizeof (t1) + sizeof (t2).  I believe
that ">=" would be right.

I'm sure the FAQ tells this in more detail.

Note added: 05/27/1997 19:30:04
By whom: conradsong@mail.utexas.edu
This is correct.  Characters do not have to be 4-byte aligned the
way integers do.

Note added: 07/27/1997 22:28:45
By whom: CraigBoston@msn.com
I think this is the same problem I encountered in bug report 0128.  GNU CC
sometimes inserts "holes" in structs to maintain proper alignment, even is you
didn't specify any alignment options on the command line.  The -fpack-struct
option should cause the struct to be the same size as the sums of its members.
Be careful with this option, though.  The docs say it can result in slower
sub-optimal code.

Closed on 04/12/1999 12:00:58: Cockpit error; nuff said.
By whom: eliz@is.elta.co.il



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