John
John

Reputation: 1119

Is the stack aligned to a 4 bytes boundary when my program starts?

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

Upvotes: 3

Views: 1072

Answers (1)

Ira Baxter
Ira Baxter

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

Related Questions