Solved

structure

Posted on 2013-06-18
10
247 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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
 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
FMX enumerated colours 2 99
Problem to scan all sheets 3 104
Dialogbox API leak? 18 94
Embarcadero WebBroker REST server endpoint parameters 2 67
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

770 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