Solved

padding tradeoff

Posted on 2013-06-02
4
279 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 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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…
The goal of this video is to provide viewers with basic examples to understand opening and reading files 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.

825 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