Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problems with #pragma pack ...

Started by vaceX Jan 10, 2004 at 8:51 AM 7 replies 550+ views
Original Post
vaceX
vaceX
What''s wrong with this code? Isn''t it suppoused to write 8 (sizeof())?
#include <iostream>

using namespace std;

#pragma pack(push, 1)

struct the_struct
{
	unsigned int	aaa			: 32;
	unsigned int	bbb			: 16;
	unsigned int	ccc			: 8;

};
#pragma pack(pop)

void
main()
{


	the_struct blahblah;
	blahblah.aaa = 1;
	blahblah.bbb = 1;
	blahblah.ccc = 1;

	cout << sizeof(blahblah) << endl;

}
That struct *should* be 7 bytes, if I''m right. I''ve also tried using #include <pshpack1.h> and #include <poppack.h> with the same results.
SiCrane
SiCrane
#pragma pack controls the packing between struct elements, it doesn''t control the padding done to structs in order to achieve proper alignment. In this case, at least on MSVC on Win32, the size of the_struct should be 8 so that arrays of the_struct have proper alignment for all elements.
Fruny
Fruny
The C++ standard says (9.6-1 [class.bit] page 159) "Alignment of bit-fields is implementation-defined." Which means that compilers are allowed to handle it in any (meaningful) way they want.

However, your question has me confused:
- "Isn''t it suppoused to write 8 (sizeof())?"
- "That struct *should* be 7 bytes, if I''m right."

Which value did you expect? And why?


“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 (C programming language co-inventor)
"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
vaceX
vaceX
quote:
Original post by Fruny
Which value did you expect? And why?



I expected it to be 7, because I thought #pragma pack worked in a different way.

Since it doesn''t work, my question is now: how do I make that struct 7 bytes?
SiCrane
SiCrane
If you really need the data to be in seven bytes, use a char array; structures are too high level to be used if you want that fine a control over your alignment and packing.
vaceX
vaceX
quote:
Original post by SiCrane
If you really need the data to be in seven bytes, use a char array; structures are too high level to be used if you want that fine a control over your alignment and packing.


Then I have to change my question again ...

How can "use bits" (like I did in that struct)inside a char array? (and together with memcpy() ?)

T2k
T2k
!?!?! why not:

struct the_struct{
unsigned int aaa;
unsigned short bbb;
unsigned char ccc;
};

!?!?


T2k
vaceX
vaceX
quote:
Original post by T2k
!?!?! why not:

struct the_struct{
unsigned int aaa;
unsigned short bbb;
unsigned char ccc;
};

!?!?


T2k


Sure, that would work. However, this was just an example. What if I want a struct that looks like this:


struct the_struct{
unsigned short aaa : 3;
unsigned short bbb : 9;
unsigned short ccc : 4;
};
?

T2k
T2k
sizeof your struct with your shorts is: 2 without pragma pack !, but you could try something like:
unsigned short a:3, b:2, c:4;

it all depends on what your compiler is doing...


T2k

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.