What is the use of padding struct in C?
2 Answers
Some architectures will perform better if only aligned accesses are made, so putting 32-bit objects on 32-bit boundaries, and 64-bit objects on 64-bit boundaries can improve the speed of your application.
Some architectures are completely incapable of making unaligned accesses, and on those architectures not padding can be a real disaster.

- 219,201
- 40
- 422
- 469
-
2Almost any architecture will perform better if you access memory on aligned boundaries ;=) – Dov Jan 03 '11 at 19:33
-
@Dov, that's probably true. But 64-bit alignment doesn't necessarily help out a 32-bit system any, and there are definitely compilers that still pad structures to align the 64-bit objects. – Carl Norum Jan 03 '11 at 19:48
-
let's clarify what we mean by architecture. If a system has a bus that transfers data n bits at a time, then doing so in a single operation takes one unit of time. Different x86 architectures may have mostly 32 bit instructions, but it's the path to memory that counts. For any architecture, forcing it to do 2 reads or writes instead of 1 is typically slower. So the relevant value is the width of the path to RAM, not whether most of the registers on the CPU process data 32 bits at a time. Intel and AMD processors have had 64 bit paths to memory for a long time now. – Dov Jan 03 '11 at 22:12
-
@Dov - I think the issue is a little more subtle than just "path to memory", probably involving where the CPU designers want to spend their transistors. IIRC the x86 32-bit instruction codes are actually in 8-bit chunks, I assume for some degree of 16-bit back-compatibility, but for the price of a few transistors, I'll bet recent chips benefited to some degree from that - being able to decode and execute more instructions at once because they are packed into fewer words, despite some alignment issues. – Jan 09 '11 at 09:23
See this Wikipedia article for more information, but basically it's to make sure that the struct occupies an exact number of bytes - which as Steve314 states means that sizeof
is an exact multiple of the alignment.
Data alignment means putting the data at a memory offset equal to some multiple of the word size, which increases the system's performance due to the way the CPU handles memory. To align the data, it may be necessary to insert some meaningless bytes between the end of the last data structure and the start of the next, which is data structure padding.
You should also know that while this was very important for a programmer to know about this, it's become less so now because it is often handled by the compiler for you. There will be compiler options that allow you to control the process though.

- 134,786
- 31
- 255
- 325
-
6It's never necessary to know the details, until something goes wrong, like a stray pointer wiping things out. Then, understanding HOW things works gives you a mental model of what could be wrong. – Dov Jan 03 '11 at 19:32
-
I have reasons to doubt my first comment - it seems a struct may (possibly must) get padding after the last member as needed to make to size an exact multiple of the alignment. As this contradicts a long-standing belief, and as I haven't found a clear statement of whether it must be done or not, I'll be asking a question shortly. – Jan 09 '11 at 04:44
-
http://stackoverflow.com/questions/4637774/is-the-size-of-a-struct-required-to-be-an-exact-multiple-of-the-alignment-of-that – Jan 09 '11 at 05:04
-
Based on answers received to the above question, if "exact number of bytes" means sizeof is an exact multiple of the alignment, this answer is correct. Sorry for the mistaken comments (now deleted), and +1 the answer. – Jan 09 '11 at 09:07