Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.


Welcome, Guest
Guest Settings
Help

Thread: pragma pack strangeness


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 4 - Last Post: Apr 4, 2017 12:00 AM Last Post By: Martin van der ...
Goran Ekstrom

Posts: 149
Registered: 1/10/04
pragma pack strangeness  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 2, 2017 12:15 PM
Hi,
if I declare this...

#pragma pack(push, 4)

typedef struct {
unsigned char NormNumber;
char Name[20];
unsigned char B_FieldEnabled;
float BFieldOne_A0;
float BFieldOne_A1;
float BFieldOne_A2;
float BFieldOne_B1;
float BFieldOne_B2;
float BFieldTwo_A0;
float BFieldTwo_A1;
float BFieldTwo_A2;
float BFieldTwo_B1;
float BFieldTwo_B2;
float BFieldThree_A0;
float BFieldThree_A1;
float BFieldThree_A2;
float BFieldThree_B1;
float BFieldThree_B2;
unsigned char E_FieldEnabled;
float EFieldOne_A0;
float EFieldOne_A1;
float EFieldOne_A2;
float EFieldOne_B1;
float EFieldOne_B2;
float EFieldTwo_A0;
float EFieldTwo_A1;
float EFieldTwo_A2;
float EFieldTwo_B1;
float EFieldTwo_B2;
float EFieldThree_A0;
float EFieldThree_A1;
float EFieldThree_A2;
float EFieldThree_B1;
float EFieldThree_B2;
}measurementnorm;

#pragma pack(pop)

... the variable "Name" ends up at the byte directly after "NormNumber". I had expected it to end up at address (&NormNumber + 4).

Using Classic compiler in Tokyo 10.2.
Valence Crearer

Posts: 64
Registered: 11/27/99
Re: pragma pack strangeness  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 2, 2017 1:18 PM   in response to: Goran Ekstrom in response to: Goran Ekstrom
Goran Ekstrom wrote:
Hi,
if I declare this...

#pragma pack(push, 4)

typedef struct {
unsigned char NormNumber;
char Name[20];
<snip/>

#pragma pack(pop)

... the variable "Name" ends up at the byte directly after "NormNumber". I had expected it to end up at address (&NormNumber + 4).

Using Classic compiler in Tokyo 10.2.

I would always have expected that byte type elements would end up at the next byte. char and bool if not treating bool as int should be placed at the next byte. Word and larger would be spread out by packing. I think this is the intended layout.

maybe someone can find the actual specification?
Perhaps: https://en.wikipedia.org/wiki/Data_structure_alignment#Data_structure_padding

Edited by: Valence Crearer on Apr 2, 2017 4:29 PM
Goran Ekstrom

Posts: 149
Registered: 1/10/04
Re: pragma pack strangeness  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 2, 2017 1:36 PM   in response to: Valence Crearer in response to: Valence Crearer
I would always have expected that byte type elements would end up at the next byte. char and bool if not treating bool as int should be placed at the next byte. Word and larger would be spread out by packing. I think this is the intended layout.

Thats not how I've learned. This is how:

http://stackoverflow.com/questions/3318410/pragma-pack-effect

After some more experimenting it seems the only pragma pack directive C++ Builder Classic follows is #pragma pack (push, 1). Any larger aligment number is simply ignored and compiles as if the directive isn't there.
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: pragma pack strangeness  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 3, 2017 1:52 PM   in response to: Goran Ekstrom in response to: Goran Ekstrom
Goran wrote:

After some more experimenting it seems the only pragma pack directive
C++ Builder Classic follows is #pragma pack (push, 1). Any larger
aligment number is simply ignored and compiles as if the directive
isn't there.

That is not true, and I have never had it behave like that. And in fact,
the VCL headers has been using "#pragma pack(push,8)" for years.

--
Remy Lebeau (TeamB)
Martin van der ...

Posts: 57
Registered: 7/14/02
Re: pragma pack strangeness  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 4, 2017 12:00 AM   in response to: Goran Ekstrom in response to: Goran Ekstrom
Goran Ekstrom wrote:
I would always have expected that byte type elements would end up at the next byte. char and bool if not treating bool as int should be placed at the next byte. Word and larger would be spread out by packing. I think this is the intended layout.

Thats not how I've learned. This is how:

http://stackoverflow.com/questions/3318410/pragma-pack-effect

After some more experimenting it seems the only pragma pack directive C++ Builder Classic follows is #pragma pack (push, 1). Any larger aligment number is simply ignored and compiles as if the directive isn't there.

It does seem to work somewhat unintuitively. From the help file I gather that character data is never aligned, and data smaller than the #pragma pack parameter is aligned on their type size. Which in practice does usually look like it's being ignored.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02