Jump to content
  • Advertisement
Sign in to follow this  
Vanderry

Unity [C++] Data structure irregularity?

This topic is 3062 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello GameDevs ! I have no doubt there is a very basic explanation to this but I was messing around with a data structure that I am going to serialize and in order to do this I thought I would rely on sizeof() for some safe data offsetting.
struct DoodadData
{
	short int	m_type[5];	// 5 short ints, 10 byte
	D3DXVECTOR3 m_position; // 3 floats, 12 byte
	D3DXVECTOR3 m_rotation;	// 3 floats, 12 byte
	float		m_scale;	// 4 byte
};

The problem is that sizeof( DoodadData ) doesn't return the same value as the summed size of the individual member variables, right now it offshoots by 2 bytes. I suspected it might be due to the mixing of arrays and single occurrences of the PODs but then it proved that if I added another copy of the short int array then the size calculation was correct. Could anyone explain this behavior please? Also, if it's not too much extra trouble, I was recommended in this thread to use a std::basic_ostringstream<unsigned char> for the serialization which seems like a nice idea. However when I try to pass any non-unsigned-char-datatype into the stream using write as suggested then I get an error: "Error 1 error C2664: 'std::basic_ostream<_Elem,_Traits>::write' : cannot convert parameter 1 from 'DoodadData *' to 'const byte_t *'". Does this suggested solution not work or is there something missing in the example code? Many thanks for all of your help, it's really speeding up the advancement of my project ! :)

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by SiCrane
What you're seeing is struct padding.


Oh, ok. Then I guess it's no real problem as long as I calculate the sum of the member variable sizes manually before allocating the byte array.

Share this post


Link to post
Share on other sites
Quote:
Original post by Vanderry
Oh, ok. Then I guess it's no real problem as long as I calculate the sum of the member variable sizes manually before allocating the byte array.


No, use sizeof, that's what it's for. The padding bytes don't necessarily need to be at the end. I suspect in you case they are added after "m_type".

If you don't want the padding, you can usually turn it off.

In MSVC you can do that by writing "#pragma pack(1)" in the line before "struct".

Note that this will make access to members of this structure a lot slower!

Share this post


Link to post
Share on other sites
Quote:
Original post by Vanderry
Quote:
Original post by SiCrane
What you're seeing is struct padding.


Oh, ok. Then I guess it's no real problem as long as I calculate the sum of the member variable sizes manually before allocating the byte array.

But that's just it, the answer isn't wrong. If you pretend it is smaller than the size it says and therefore allocate less memory then you will get a buffer overrun.

I would move m_type to the end, and if I was desperate to save 6% off the size of this struct then I would consider using #pragma pack(2) for it.
I'd recommend reading a bit more about structure padding.

For the second thing, you're just supposed to cast it as required.

Share this post


Link to post
Share on other sites
Quote:
Original post by iMalc
But that's just it, the answer isn't wrong. If you pretend it is smaller than the size it says and therefore allocate less memory then you will get a buffer overrun.

I would move m_type to the end, and if I was desperate to save 6% off the size of this struct then I would consider using #pragma pack(2) for it.
I'd recommend reading a bit more about structure padding.


I should probably have mentioned that I intend to write the composed data to a binary file (and I wouldn't want to store any unnecessary "data"). If I'm not completely confused now, data structure padding shouldn't occur on a file in the main memory, or?

Quote:
Original post by iMalc
For the second thing, you're just supposed to cast it as required.


Thanks, I thought I had tried that already but it must have been late in the night or something xP

[Edited by - Vanderry on February 28, 2010 1:36:44 PM]

Share this post


Link to post
Share on other sites
It really depends on how you're doing it. If you write each data member individually to the file then the padding shouldn't show up in the file. If you dump the structure all in one go it probably will.

Share this post


Link to post
Share on other sites
Have you considered just writing each data member separately? Or maybe even doing some more sophisticated form of serialization?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!