sizeof padding

Started by
7 comments, last by JohnG 21 years, 12 months ago
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]
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
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.
---visit #directxdev on afternet <- not just for directx, despite the name
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__.

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.
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]
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.
---visit #directxdev on afternet <- not just for directx, despite the name
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

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

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.

This topic is closed to new replies.

Advertisement