• Advertisement

Archived

This topic is now archived and is closed to further replies.

sizeof padding

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

Is there a way I can get the size of a structure without having to manually calculate it? I can't use "sizeof" because it compensates for compiler padding. What I did find was a post that mentioned using #pragma around your structure definition like so: #pragma pack (push, 1) struct TEST { // Data members here }; #pragma pack (pop) Then sizeof works. But is this something I should use or is there a better way of doing it? [edited by - JohnG on April 17, 2002 6:48:08 PM]

Share this post


Link to post
Share on other sites
Advertisement
thats a decent way of doing it, im pretty sure GCC does it as well. you could always do a sizeof on all the members of the class too, kinda inconvenient since you''d have to change your formula if you edited the class

Share this post


Link to post
Share on other sites
If by "size of a structure" you mean "how much memory this struct occupies", then whatever value is returned by sizeof is correct. Padding pads memory, and if you have a five-byte struct that''s padded to eight bytes, its size in memory will be eight, and not five, bytes.

If you want to use this struct for ie reading stuff from file, you have to disable padding so that you read correctly.

Share this post


Link to post
Share on other sites
quote:
Original post by barazor
thats a decent way of doing it, im pretty sure GCC does it as well.

The last time I tried the MSVC-style padding pragma in GCC it gave weird results. They''ve probably fixed it since then though. For information on GCC''s native style of padding manipulation look for information on __attribute__.

Share this post


Link to post
Share on other sites
What do you want to know the size of the structure for? Most of the time, whatever sizeof gives you is what you actually want (padding and all)


codeka.com - Just click it.

Share this post


Link to post
Share on other sites
I want to use sizeof for reading data from a file.

For example:


      
fin.read (reinterpret_cast<char *> (&ChunkDescriptor), sizeof(CHUNK_DESCRIPTOR));


This would not work without the above #pragma code around my CHUNK_DESCRIPTOR structure.

[edited by - JohnG on April 18, 2002 12:44:01 PM]

Share this post


Link to post
Share on other sites
You want to read a piece of data from file to memory, byte-by-byte. In order to accomplish this, layout of data that you''re reading in memory must match the layout of that data on disk. Unless you disable packing, the layouts won''t match, because a padded struct is not the same as unpadded struct. You have to turn packing off, otherwise your code won''t work.

Share this post


Link to post
Share on other sites
Another idea is to arrange your structures such that each element lines up properly except for maybe the last element, like so:

  
typedef struct
{
ULONG ulSomeVar;
ULONG ulSomeVar2;
UWORD uwSmallerVar;
UCHAR ucArray[5];
} tMyStruct;


It''s how I do it for certain structures that I have to store to and from file. It might not be so easy for you, but it''s worth you thinking about.

Nutts

Share this post


Link to post
Share on other sites
If the same program writes the file as the program that reads it (for example, you''re writing a "save game" feature) then you don''t really have to worry about padding, since it''s going to be the same. You only worry about padding when two different programs read/write the file. Even then, you don''t really need to worry if you''re using the same compiler for both the reader and the writer (since it''ll pad the structures the same in both programs).



codeka.com - Just click it.

Share this post


Link to post
Share on other sites

  • Advertisement