Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

What is structure padding ?

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the primitive data types in c?

1006


How do we make a global variable accessible across files? Explain the extern keyword?

1827


What is the difference between struct and typedef struct in c?

1082


How a string is stored in c?

1016


Tell me about low level programming languages.

1067


What is the total generic pointer type?

1108


code for quick sort?

1984


What do you understand by normalization of pointers?

1005


Explain the advantages of using macro in c language?

951


What does typedef struct mean?

1044


What are the valid places to have keyword “break”?

1039


in case any function return float value we must declare a) the function must be declared as 'float' in main() as well b) the function automatically returned float values c) function before declared 'float' keyword d) all the above

974


Where are c variables stored in memory?

1022


Why is C language being considered a middle level language?

1042


Why isn't it being handled properly?

1001