struct {
integer a;
struct c b;
...
}
In general how does gcc calculate the required space? Is there anyone here who has ever peeked into the internals?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I have not “peeked at the internals”, but it’s pretty clear, and any sane compiler will do it exactly the same way. The process goes like:
Here’s an example (assume
intis 4 bytes and has 4 byte alignment):char(1); size is still 0.char(1); size is now 1.int(4); size is now 4.int(4); size is now 8.char(1); size is still 8.char(1); size is now 9.Edit: To address why the last step is necessary, suppose instead the size were just 9, not 12. Now declare
struct foo myfoo[2];and consider&myfoo[1].b, which is 13 bytes past the beginning ofmyfooand 9 bytes past&myfoo[0].b. This means it’s impossible for bothmyfoo[0].bandmyfoo[1].bto be aligned to their required alignment (4).