Advertisement

Linux sizeof different

Started by April 20, 2005 02:46 AM
7 comments, last by Anon Mike 19 years, 7 months ago
I have a structure:

struct TriangleList
{
	union
	{
		int Vertices[3];

		struct
		{
			int Vertex1,Vertex2,Vertex3;
		};
	};
	union
	{
		int TextureUV[3];

		struct
		{
			int Uv1,Uv2,Uv3;
		};
	};
	
	struct
	{
		union
		{
			double Normals[3];

			struct
			{
				double Normal1, Normal2, Normal3;
			};
		};
	} NormalCoords[3];

	int MaterialNumber;
};
When compiled in C++.Net 2003, the sizeof(TriangleList) is 104. When compiled in GCC, the sizeof(TriangleList) is 100. Anyone know why this is?
That's strange, especially since I count 64 bytes:
3*int = 3*4 = 12
3*int = 3*4 = 12
3*3*double = 3*3*4 = 36
int = 4

12+12+36+4 = 64
Advertisement
Quote: Original post by rick_appleton
That's strange, especially since I count 64 bytes:
3*int = 3*4 = 12
3*int = 3*4 = 12
3*3*double = 3*3*4 = 36
int = 4

12+12+36+4 = 64


double is 8 bytes large, so that should be :

3*int = 3*4 = 12
3*int = 3*4 = 12
3*3*double = 3*3*8 = 72
int = 4

total = 100

It probably has something todo with byte alignment. MSVC probably aligns on 8 byte boundaries hence the extra 4 bytes. (104 is dividable by 8, 100 isn't)
"THE INFORMATION CONTAINED IN THIS REPORT IS CLASSIFIED; DO NOT GO TO FOX NEWS TO READ OR OBTAIN A COPY." , the pentagon
Indeed, my bad.
Weird, it's the int MaterialNumber; causing to add 8 bytes, but shouldn't actually? The struct is nicely on a 32 bit boundary.

Using pragma pack(1) will make the size be 100 on MSVC too, but i don't really see where the padding should be.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

I'd argue between the second union and the struct.

You can use the offsetof macro to get the offsets of each and every member.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
Maybe alignment is the reason.

If you create an array of TriangleList, then the double members still need 8 byte alignment. So the structure is padded to make its size a multiple of 8, which is 104 = 8 * 13. And GCC is incorrect in this case.
Thanks for the help. It is definitly an alignment issue. So I fixed it by putting in a float after int MaterialNumber and now it is 104 on both. So i guess MSVC was padding to the end of the struct. So now they both act exactly the same.

Because you have an element that is 8-byte naturally aligned (the doubles) MSVC is padding the structure out to a multiple of 8 bytes. Presumably so that if you have an array of them all the elements of the array would be aligned properly instead of just every other one.

I'm a bit suprised gcc doesn't do that also. What does gcc give for sizeof(TriangleList[2]) and is the second element aligned properly?
-Mike

This topic is closed to new replies.

Advertisement