Whats is structure padding?Say a given structureStruct{int a;char c;float d;}the size of structure is 7 here.But structure padding is done what will be the size of the struct?Will it change and how?How to avoid this?is it necessary?

Showing Answers 1 - 12 of 12 Answers

NileshYadav

  • Jul 17th, 2006
 

integers and floats :compilers will try to place these variables at addresses which are in multiples of 2 or 4(in 16-bit system) now in this case 1 byte can be padded...we can store integers and floats at start to avoid padding

  Was this answer useful?  Yes

meenakshi vishnoi

  • Jul 30th, 2006
 

the answer is 7

bytes of int=2

char=1

float=4

2+1+4=7

  Was this answer useful?  Yes

shaanxxx

  • Aug 19th, 2006
 

you can control padding using pragma pack why you need padding ? Ans is you need to align your VAR accordning to word boundary , padding will make your programne work fast , If you dont have padding , use of above struct will give bus error on some machine. To understand that we need go through more document on this .Thanksshaan

  Was this answer useful?  Yes

sravan k

  • Feb 20th, 2007
 

the given structure is:

Struct{
int a;
char c;
float d;
}

the memory allocated to this structure is(with out padding): 7.
the memory allocated to this structure is(after padding):8.
why because padding means "when ever u needs to access byte by byte information then u have to do padding". After padding strudture becomes:


Struct{
int a;
char c;
char pad;
float d;
}

here i would like to add a member of type char. Because due to adding this member
we can access iinformation byte by byte.

dasam

  • Mar 31st, 2007
 

structure padding problem can be avoided by using __atribute__((packed)) after each element in the structure.

  Was this answer useful?  Yes

dasam

  • Mar 31st, 2007
 

we can do __attribute__((packed)) but be careful the code is not portable to some operating systems.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions