ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
tip   SiteMap shows list of All Categories in this site.
Google
 
Categories  >>  Software  >>  Programming Languages  >>  C
 
 


 

 
 C interview questions  C Interview Questions
 C++ interview questions  C++ Interview Questions
 VC++ interview questions  VC++ Interview Questions
 Delphi interview questions  Delphi Interview Questions
 Programming Languages AllOther interview questions  Programming Languages AllOther Interview Questions
Question
What is structure padding ?
 Question Submitted By :: Leossk
I also faced this Question!!     Rank Answer Posted By  
 
  Re: What is structure padding ?
Answer
# 1
[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 ?    3 Yes 0 No
Vijoeyz
 
  Re: What is structure padding ?
Answer
# 2
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 ?    0 Yes 0 No
Priya
 
 
 

 
 
 
Other C Interview Questions
 
  Question Asked @ Answers
 
what is the differance between pass by reference and pass by value. Infosys4
Find string palindrome 10marks Honeywell5
write a program for even numbers?  8
program for comparing 2 strings without strcmp()  3
write a program that explain #define and # undef directive  1
what is use of loop? Infosys6
How can I invoke another program from within a C program?  6
swap two integer variables without using a third temporary variable?  2
Write a C Programm.. we press 'a' , it shows the albhabetical number is 1, if we press 'g' it shows the answer 7.. any can help me  4
Write a C function to search a number in the given list of numbers. donot use printf and scanf Honeywell6
what's the o/p int main(int n, char *argv[]) { char *s= *++argv; puts(s); exit(0); } Motorola1
What should not contain a header file?  2
how to find the size of the data type like int,float without using the sizeof operator?  7
Write a program or provide a pseudo code to flip the 2nd bit of the 32 bit number ! (Phone Screen) NetApp1
What is the output for the following program #include<stdio.h> main() { char a[5][5],flag; a[0][0]='A'; flag=((a==*a)&&(*a==a[0])); printf("%d\n",flag); } ADITI5
main() { char *p; p="Hello"; printf("%c\n",*&*p); } ME2
how to find out the reverse number of a digit if it is input through the keyboard?  2
write a program to swap bits in a character and return the value prototype of function char fun (char a, charb flag c) where fun returns a char, char a is a the value char b is the bit to be changed and flag c is the bit value for eg: x=fun(45,7,0) since 45 is 0010 0101 and ow x should contain the value 65 (0110 0101) Bosch1
how many header file is in C language ?  14
HOW DO YOU HANDLE EXCEPTIONS IN C? AppLabs2
 
For more C Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com