How size of structure can be calculated? What is byte alignment used while allocating memory for structure members.

Questions by svyengalwar

Showing Answers 1 - 15 of 15 Answers

Mathi

  • Apr 18th, 2007
 

The size of the structutre will be the total size of fields holded by that structure.For Eg
struct emp
{
   int empid;
  char name[20];
  float bpay;
}e;
 sizeof (e) =   sizeof(empid)+sizeof(name[20])+sizeof(bpay)=2+20+4=26.
 

  Was this answer useful?  Yes

J

  • Nov 22nd, 2007
 

Size for the above example would be 28

4 + 20 + 4

  Was this answer useful?  Yes

prabhjots

  • Feb 6th, 2008
 

hey first of all i got this as 26 (running on windows)..
and the other thing you are asking about memory aligment is dat there are restrictions by some compilers(or by machines) that every int (or float ) should start from an adress which should be divisible by 2 or 4(bcoz of some performance issues). i have heard dat it is not true for intel processors(but not sure)., so on those machines the size of structure can be different if it has used some "padding bytes or alignement bytes"
hope this clears some of your doubts

  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