Solved

padding tradeoff

Posted on 2013-06-02
4
281 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: 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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

730 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