Reputation: 1119
I want to make sure that my variables are correctly aligned. So I have the following questions:
When my program first starts, i.e. when my entry point starts executing, is the stack at this point aligned to a 4 bytes boundary? Or does that depends if I am linking against the CRT or not (I would guess that the CRT has its own entry point that calls my entry point, so this CRT entry point could adjust the stack alignment, or is an entry point added to my program either way?). Or I should not make any assumptions about the alignment of the stack and check the esp value myself?
When I call a function, does this function expects the stack to be aligned, or does that depends on the calling convention?
Upvotes: 3
Views: 1072
Reputation: 95306
On 32 bit systems, ESP passed to your program entry point is 4-byte aligned. On 64 bit systems, it is 8 byte aligned. This is guaranteed by the program loading/startup logic, which passes such an aligned ESP to your entry point.
Once your program starts execution, all functions (library, OS calls, and the ones you will code) expect this alignment to be preserved, and virtually no code violates it. (One reason why: misaligned stacks at best means misaligned accesses, which slows your program down.)
Upvotes: 3