What is structure padding ?

Answers were Sorted based on User's Feedback



What is structure padding ?..

Answer / vijoeyz

[See http://www.geocities.com/vijoeyz/faq/c/padding.txt]

All modern CPUs expect that the fundamental types --
int's, float's and
long's -- are stored in the memory at their natural
boundary; typically, at
addresses that are multiples of their length. Some CPU work
efficiently if the
memory is properly aligned, and some can work in either case.

For the following examples, let us assume:

sizeof (int) == 4
sizeof (char) == 1
sizeof (float) == 4

When a C compiler processes a structure, it adds padding
bit(s)/byte(s), if
required, between the members to ensure proper alignment.
Consider the
following scenario:

> What is the difference between the following structures:
>
> struct pad1
> {
> int a;
> char c;
> float f;
> };
>

"a" and "f" should occur at an address multiple of 4,
whereas "c" can take
any -- odd or even -- address. So, the structure appears in
the memory as
shown:

___________________
| a0 | a1 | a2 | a3 | 4-byte alignement
------------------- P is padding byte
| c0 | P0 | P1 | P2 |
-------------------
| f0 | f1 | f2 | f3 |
-------------------

> and
>
> struct pad2
> {
> float f;
> int a;
> char c;
> };
>
___________________
| f0 | f1 | f2 | f3 | 4-byte alignement
------------------- P is padding byte
| a0 | a1 | a2 | a3 |
-------------------
| c0 | P0 | P1 | P2 |
-------------------

Following point are worth noting:

* The compiler also ensures that the structure as a
whole appears at an
aligned address.

* Padding does NOT occur at the beginning of a structure.

* The value of padding bytes or bits are
implementation defined.

> What is the use of padding?

* Padding is useful, for example, in conforming to
externally imposed
layouts of machine registers.

* The obvious advantage is efficient access by CPU.


> And finally what is ring buffer?Where is it used.Someone
pls. explain
> in detail.

* A buffer of data which is of fixed size; when it
fills, further data is
placed back at the start of the buffer,
overwriting the old data,
in a "ring". Commonly used in device drivers.

For more examples, use the Google the keyword "define:
ring buffer",
excluding the quotes.

Is This Answer Correct ?    32 Yes 2 No

What is structure padding ?..

Answer / priya

structure padding is used to pad the data in
such a way that it can be sent to external devices.
Sometimes, the data is padded in such a way
that it can be used on little endian vs big
endian processors
Padding is done to fast access of data from memory.

Is This Answer Correct ?    14 Yes 8 No

What is structure padding ?..

Answer / anil

* Padding is useful, for example, in conforming to
externally imposed
layouts of machine registers.

* The obvious advantage is efficient access by CPU.

Is This Answer Correct ?    4 Yes 2 No

Post New Answer

More C Interview Questions

what is c programming?

3 Answers   TCS,


what is the use of a array in c

6 Answers  


In this assignment you are asked to write a multithreaded program to find the duplicates in an array of 10 million integers. The integers are between -5000,000 to 5000,000 and are generated randomly. Use 10 threads, each thread works on 1000,000 integers. Compare the time needed to accomplish the task with single thread of execution program. Do not include the time to fill the array with integers in the execution time.

0 Answers  


Where register variables are stored in c?

0 Answers  


Explain what is the difference between text files and binary files?

0 Answers  






Define VARIABLE?

0 Answers   ADP,


what do you mean by inline function in C?

0 Answers   IBS, TCS,


what is memory leak?

3 Answers  


List the difference between a "copy constructor" and a "assignment operator"?

0 Answers   Accenture,


totally how much header files r in c language

8 Answers   TCS,


Differentiate Source Codes from Object Codes

1 Answers  


Why can arithmetic operations not be performed on void pointers?

0 Answers  


Categories