Page 1 of 1

Local Variable Storage

Posted: Thu Jun 07, 2007 2:56 pm
by Tyler
In an x86 Setup, Compiling with a common C Compiler, are local automatic variables, regardless of size, all stored as a single 32-bit entries on the stack?

Also, are Structs and classes at procedural level pushed onto the stack or Allocated elsewhere in memory. This is for automatic variables only.

Posted: Thu Jun 07, 2007 3:14 pm
by nick8325
They might be. Why do you need to know?

I just tested, and GCC seems to store characters 32-bit aligned (with optimisation off, at least). But the compiler might well keep a variable in a register, instead of on the stack.

Structs and classes will be pushed onto the stack, unless they're kept in registers (or removed altogether, if the compiler decides they're not needed).

Posted: Thu Jun 07, 2007 3:28 pm
by mathematician
In C the register keyword is supposed to give the compiler a strong hint that it should store a variable in a register, but Visual C for one ignores that, and stores whatever variables it sees fit in registers.

Arguments passed to a function are generally pushed onto the stack starting with the rightmost argument first. Some compilers (Watcom for one) will use registers, but you can usually turn that feature off if you want to.

Posted: Fri Jun 08, 2007 4:28 am
by Tyler
I need to know because i will be forceably linking some assembly to the end of some functions, pre the epilogue. I wanted to make sure that chances were they were compiled as 32-bit and not optimised to best fit two 16-bits in a single slot or anything stupid like that.

Posted: Fri Jun 08, 2007 5:08 am
by mathematician
Could you compile a short test program and then put it through a disassembler or debugger to see what the compiler has done with it?

Posted: Fri Jun 08, 2007 5:16 am
by Tyler
mathematician wrote:Could you compile a short test program and then put it through a disassembler or debugger to see what the compiler has done with it?
Yes i could and have... but i just wondered if anyone knew the standard before i have to do it with loads to get the average. This is because i won't know what the code has been compiled with, and won't be able to check all the code to see how it works. Onwards with the disassembling i guess.