Bug 21361 - sizeof() packed structs potential errors
Summary: sizeof() packed structs potential errors
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.0.0
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-05-03 15:09 UTC by Marc 'Foddex' Oude Kotte
Modified: 2007-11-15 16:45 UTC (History)
2 users (show)

See Also:
Host: i686-redhat-linux
Target: i386-redhat-linux
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Marc 'Foddex' Oude Kotte 2005-05-03 15:09:51 UTC
The following code produces different output in gcc4.0.0 and MSVC++7 .NET.
Additionally, I've tried 3.4.3 20050227 (Red Hat 3.4.3-22.fc3) and gcc 3.3.4
20040817 (Red Hat Linux 3.3.4-2), who all produce the same output as gcc4.0.0.

I'm unsure who is right here (don't know the exact C++ standards). If gcc4 is
right and MSVC++ is wrong this bug is invalid. 

CODE:
#include <stdio.h>

#define DEFSTR(name) \
	typedef struct name { \
		unsigned int var : 13; \
	};

#pragma pack(push, 1)
DEFSTR(pack1_struct)
#pragma pack(pop)

#pragma pack(push, 2)
DEFSTR(pack2_struct)
#pragma pack(pop)

#pragma pack(push, 4)
DEFSTR(pack4_struct)
#pragma pack(pop)

#pragma pack(push, 8)
DEFSTR(pack8_struct)
#pragma pack(pop)

#pragma pack(push, 16)
DEFSTR(pack16_struct)
#pragma pack(pop)

DEFSTR(unpacked_struct)

int main( int argc, char** argv ) {

	printf("sizeof(pack1_struct) = %u\n", sizeof(pack1_struct) );
	printf("sizeof(pack2_struct) = %u\n", sizeof(pack2_struct) );
	printf("sizeof(pack4_struct) = %u\n", sizeof(pack4_struct) );
	printf("sizeof(pack8_struct) = %u\n", sizeof(pack8_struct) );
	printf("sizeof(pack16_struct) = %u\n", sizeof(pack16_struct) );
	printf("sizeof(unpacked_struct) = %u\n", sizeof(unpacked_struct) );
	return 0;
}

PRODUCES ON GCC:
sizeof(pack1_struct) = 2
sizeof(pack2_struct) = 2
sizeof(pack4_struct) = 4
sizeof(pack8_struct) = 4
sizeof(pack16_struct) = 4
sizeof(unpacked_struct) = 4

PRODUCES ON MSVC++7:
sizeof(pack1_struct) = 4
sizeof(pack2_struct) = 4
sizeof(pack4_struct) = 4
sizeof(pack8_struct) = 4
sizeof(pack16_struct) = 4
sizeof(unpacked_struct) = 4
Comment 1 Marc 'Foddex' Oude Kotte 2005-05-04 10:27:52 UTC
I noticed the PCC_BITFIELD_TYPE_MATTERS setting (hint:
http://www.astro.cf.ac.uk/cgi-bin/info2www?(gcc)Storage%20Layout ). I guess
packing settings overrule this PCC_BITFIELD_TYPE_MATTERS setting? If it was
meant to do so (and not just accidentally happened to do so), then please close
this bug. 
Comment 2 Manuel López-Ibáñez 2007-11-15 16:45:07 UTC
Closing then.

(The link you gave is broken. A more stable link is
 http://gcc.gnu.org/onlinedocs/gccint/Storage-Layout.html
)