?
Solved

padding tradeoff

Posted on 2013-06-02
4
Medium Priority
?
304 Views
Last Modified: 2013-06-04
Compilers do padding of struture members to improve the performance with the expense of memory.  (its a well-known trade-off)

Example)

typedef struct {
      uint32_t a;   //4 byte
      uint64_t b;   // 8 byte
} TEST;

On a 32 bit machine, sizeof(TEST) will be 12.
On a 64-bit machine, sizeof(TEST) will be 16. (because of the extra padding added for after member a)

1) What is the default behavior of compiler in gcc? Will it add paading by default or will it complie it a packed structure without padding? Is there a #define to contol this behavior?

2) Lets say if it is adding padding, does it "zeroed" those extra padded bytes? How would the runtime know how mich data to be read for member "a" and what is the starting address of member "b" as there is 4 bytes padded after "a"
0
Comment
Question by:perlperl
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 39214503
The default behaviour is to add packing - if you don't want that, you can turn that off or fine-tune the behaviour by using '-fpack-struct[=n]' (see also the docs at http://www.freebsd.org/cgi/man.cgi?query=gcc&apropos=0&sektion=0&manpath=FreeBSD+7.0-RELEASE&format=html)

And to address the 2nd part of your question: The runtime does not care if *your* structs are padded otr not, since it is solely your code (compiled by gcc/g++) that accesses it, and every code that is supposed to deal with these has be properly instrumentated by gcc/g++ during the compile phase.
0
 

Author Comment

by:perlperl
ID: 39214567
i am little confused.
so basically if we dont specify any option to gcc during compile time, it will do "PADDING" for struct members for performance optimization. Correct?
0
 
LVL 86

Expert Comment

by:jkr
ID: 39214573
Yes, that's right. IMO turning off padding nowadays only makes sense on embedded systemms with extremely low amounts of memory, and you'll hardly encounter these "in the wild" any more.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 39214760
To reduce padding in large structures, cluster the largest data types first (e.g., double), followed by a cluster of the next largest data type (e.g., float or long), and so on down to char. One area where you may want to add a lot of padding is when you are working in a multithreading program and have locking and shared data variables near each other. You may want to add, say, char padding[128], between the locking variable (e.g., semaphore or mutex) and the shared data variable so that the synchronization variables are in a different cache line than the shared variable. Then, when one thread modifies the shared variable, the other threads do not have to invalidate their cache lines for the synchronization variable.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops 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.
Suggested Courses

809 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