Jump to content
  • Advertisement
Sign in to follow this  
zalthar

Sizeof

This topic is 5411 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

Why does a sizeof of this struct return 20? The struct is only 8 bytes long? How should I do to get the right size?
'
struct tga_file_header
{
	char id;
	char colorMapType;
	char imageType;
	unsigned short firstEntryIndex;
	unsigned short colorMapLength;
	char colorMapEntrySize;
	unsigned short xOrigin;
	unsigned short yOrigin;
	unsigned short width;
	unsigned short height;
	char pixelDepth;
	char imageDescriptor;
};

Share this post


Link to post
Share on other sites
Advertisement
That structure is not 8 bytes long. Even without packing it is still 18 bytes, assuming 2 byte short. The extra two bytes are padding on the 3rd char and the 4th char to align things nicely.

Finally, it's fine. Don't skrew with padding. Instead, read in each field individually. Skrewing with packing is a compiler specific option, and will make your code harder to maintain.

Share this post


Link to post
Share on other sites
Read in each field individually. Skrewing with packing is a compiler specific option, and will make your code harder to follow.

Share this post


Link to post
Share on other sites
I assume you use VS .NET ?

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/pragm.asp

The structures are usually 4 byte aligned by default on a 32 bit compiler (for speed reasons). It appears to be adding the bytes to pad your short variables to 2 byte boundaries (so between imageType and firstEntryIndex it will add a byte and between ccolorMapEntrySize and xOrigin).

Share this post


Link to post
Share on other sites
Go ahead and keep them in a struct. But what do you need them to be 18 bytes for? If it's so that you can write them out to a file, or read them in from a file, then read/write each value at a time. But you can still keep them in a struct. Just don't try to write out the entire struct all at once, exactly as it is in memory.

[edit]I just noticed that I didn't need to ask "what do you need them to be 18 bytes for?" since it is obvious that it's to read a TGA header from a file. Sorry for the hastiness. Advice still applies, though.[/edit]

Share this post


Link to post
Share on other sites
There should be no problem in reading in and writing the entire structure(give it a quick try...). However, I would read the reference on packing just to familiarize yourself. One thing you may want to consider is to group your bigger variable types together in the beginning of your structure (this is how I was told to do this moons ago...):


struct tga_file_header
{
unsigned short firstEntryIndex; // 2
unsigned short colorMapLength; // 2
unsigned short xOrigin; // 2
unsigned short yOrigin; // 2
unsigned short width; // 2
unsigned short height; // 2
char colorMapEntrySize; // 1
char id; // 1
char colorMapType; // 1
char imageType; // 1
char pixelDepth; // 1
char imageDescriptor; // 1
};



But as stated before, this may make your code less readable. Either way, as long as the data that is written out and read in is aligned the same way you should be in good shape.


Hope that helps,

#dth-0

Share this post


Link to post
Share on other sites
Well I have tried and when I read in the entire structure the bytes are not read correctly. But when I read them variable by variable in the same order as before there is no problem.
I can't sort them as you did becouse they are already sorted in a special way in the .tga file (The graphics file format).

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!