Local Variable Storage

Programming, for all ages and all languages.
Post Reply
Tyler
Member
Member
Posts: 514
Joined: Tue Nov 07, 2006 7:37 am
Location: York, England

Local Variable Storage

Post 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.
nick8325
Member
Member
Posts: 200
Joined: Wed Oct 18, 2006 5:49 am

Post 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).
User avatar
mathematician
Member
Member
Posts: 437
Joined: Fri Dec 15, 2006 5:26 pm
Location: Church Stretton Uk

Post 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.
Tyler
Member
Member
Posts: 514
Joined: Tue Nov 07, 2006 7:37 am
Location: York, England

Post 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.
User avatar
mathematician
Member
Member
Posts: 437
Joined: Fri Dec 15, 2006 5:26 pm
Location: Church Stretton Uk

Post 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?
Tyler
Member
Member
Posts: 514
Joined: Tue Nov 07, 2006 7:37 am
Location: York, England

Post 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.
Post Reply