Is the stack aligned with a 4 byte boundary when I run my program?

I want to make sure my variables are correctly aligned. Therefore, I have the following questions:

  • When my program starts, that is, when my Execution entry point begins, is the stack at this point consistent with a 4 byte boundary? Or it depends if I am connected to the CRT or not (I would assume the CRT has its own entry point that calls my entry point, so this CRT entry point can adjust the stack alignment or the entry point is added to my program anyway?). Or shouldn't I have any guesses regarding stack alignment and checking the esp value myself?

  • When I call a function, does this function expect the stack to be aligned or does it depend on the calling convention?

+3


source to share


1 answer


On 32-bit systems, the 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 bootstrap / bootstrap logic that passes such an aligned ESP to your entry point.



Once your program starts, all the functions (library, OS calls, and whatever you specify) expect this alignment to be preserved and virtually no code will break it. (One reason: unbalanced stacks mean mishandles at best, which slows down your program.)

+3


source







All Articles