When I write:
int a;
int c;
OR
int a, c;
- When are
aandcstored in adjacent places in the memory? ((&a+1)equals&c?) - Does the way you define them has influence on this? Or it only depends on the machine?
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.
No, you cannot guarantee that
aandcare in adjacent places in memory, but that is usually the case. I believe usually&a - 1will be equal to&cas the stack usually grows downwards.If you want contiguous variables then use an array.
Usually the compiler won’t reorder unless it has to. For instance to meet certain alignments or for performance reasons.
Also, as Mysticial says, the compiler can sometimes optimize variables out so they never exist in memory at all.