Solved

padding tradeoff

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ get user from AD  (VS6) 7 53
Exception thrown at 0x00007FFD5BC81F28 7 38
How to install SVN Command Line Client? 5 42
Computer slow / BSOD 10 42
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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…
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now