Solved

structure

Posted on 2013-06-18
10
246 Views
Last Modified: 2013-07-06
How does the compiler pads a structure? Does it always pads sequentially?
Consider a 64 bit machine


struct PAD {
   uint32_t a;
   uint32_t b;
   uint64_t c;
   uint32_t d;
   // Will it add 4 bytes padding at the end or can it pad anywhere?
}
0
Comment
Question by:perlperl
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 45

Accepted Solution

by:
Kdo earned 500 total points
ID: 39257778
It depends mostly on the compiler options.

The selected alignment may cause the items in the structure to be packed (no padding) or aligned at the 16, 32, or 64 bit mark.  If alignment is at 64 bits, there will be padding after every item that is shorter than 64 bits, or any array that is isn't a multiple of 64 bits.



Good Luck,
Kent
0
 
LVL 86

Expert Comment

by:jkr
ID: 39257780
Padding always occurs at the end of a structure, otherwise that would not make sense at all. In your above case, no padding will occur anyway, since the structure already is 4-byte-aligned. It would be different if it was like this:

struct PAD {
   uint32_t a;
   uint32_t b;
   uint64_t c;
   uint32_t d;
   char f;
   // 3 padding bytes will be added here unless specified differently
} 

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 39257805
Each non-bit-field member of a structure or union object is aligned in an implementationdefined
manner appropriate to its type.
0
 

Author Comment

by:perlperl
ID: 39257919
in my case it does add 4 bytes padding
0
 
LVL 86

Expert Comment

by:jkr
ID: 39257928
I don't think so - the struct size is 16 bytes, and that should be what 'sizeof()' reports.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 45

Expert Comment

by:Kdo
ID: 39258109
Hi perl,

What you're probably seeing is that the structure is packed (no padding between the variables) and a total of 20 bytes in length.

  PAD pad_array[2];

Then depending on the data alignment option, the array above will pad the array so that each item starts on a 64-bit boundary.


Kent
0
 
LVL 86

Expert Comment

by:jkr
ID: 39258186
Kdo,,

a 20-byte sized struct isn't really well-aligned for 64 bit adresses either... something is quite confusing about this whole setup.
0
 
LVL 45

Expert Comment

by:Kdo
ID: 39258997
Hi jkr,

I think that it can be explained simply by data alignment and structure blocking.  If data alignment is to the word (64 bits) and structures are not packed, there will be a half word of padding after each 32-bit entity.  

If alignment is to the half-word (32 bits) or packed structures are forced due to a PRAGMA or other compiler option, then there will be no padding in the structure.


Kent
0
 
LVL 84

Expert Comment

by:ozo
ID: 39261206
You can use the offsetof and sizeof macros to determine where and whether padding was added in your implementation.
0
 
LVL 33

Expert Comment

by:sarabande
ID: 39283740
at win7-64 bit  vc compiler with 8-byte alignment i have

struct PAD {
   UINT32 a;
   UINT32 b;
   UINT64 c;
   UINT32 d;
//   char e;
}; 

int main(int argc, char **argv)
{
    PAD pad;
    std::cout << (int)((UINT64)(void*)&pad.b-(UINT64)(void*)&pad.a) << "|" 
              << (int)((UINT64)(void*)&pad.c-(UINT64)(void*)&pad.a) << "|"
              << (int)((UINT64)(void*)&pad.d-(UINT64)(void*)&pad.a) << "|"
//              << (int)((UINT64)(void*)&pad.e-(UINT64)(void*)&pad.a) << "|"
              << (int)(sizeof(pad)) << std::endl;

Open in new window


the output was

4|8|16|24

what pretty much is that i expected.

with the char member e, the output was

4|8|16|20|24

what also is consequent.

the rule is that padding was done if next member would not begin at an alignment fitting to its own size or would overlap the required alignment. so an 4-byte integer always would begin at a 4-byte segment. a single char can begin at next byte regardless of alignment.  

struct PAD 
{
   UINT32 a;
   UINT32 b;
   UINT64 c;
   char d[5];
   char e;
   short f;
   UINT32 g;
};

Open in new window


4|8|16|21|22|24|32

you see the char e begins at an odd byte and short f very well can be put packed between e and g.

Sara
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maze travler 6 51
Would like to move button in a function 3 72
Find Visual Studio Tools 2 92
Least Squares Curve Fitting 4 60
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now