Stack alignment question
Posted: Wed Jun 18, 2003 1:36 am
Hi,
This is probably bit of a stupid question but:
For x86 the stack is 32bit aligned. When you compile a C application local function variables are placed on the stack. What happens if the local variable is say 8 bytes? Does the compiler (GCC for example) ensure that the 8byte variable is naturally aligned? Or does GCC just put the variable on the stack unaligned.
example code:
int foo(int input)
{
int testVal;
long long testVal2; //Is this how to declare a 64bit int?
//Yes I know this is a stupid function
testVal = input;
testVal2 = (long long)testVal;
return (int)testVal2;
}
Does GCC ensure testVal2 is aligned to 8 bytes, or could it possibly be misaligned at an address which is an odd multiple of 4?
This is probably bit of a stupid question but:
For x86 the stack is 32bit aligned. When you compile a C application local function variables are placed on the stack. What happens if the local variable is say 8 bytes? Does the compiler (GCC for example) ensure that the 8byte variable is naturally aligned? Or does GCC just put the variable on the stack unaligned.
example code:
int foo(int input)
{
int testVal;
long long testVal2; //Is this how to declare a 64bit int?
//Yes I know this is a stupid function
testVal = input;
testVal2 = (long long)testVal;
return (int)testVal2;
}
Does GCC ensure testVal2 is aligned to 8 bytes, or could it possibly be misaligned at an address which is an odd multiple of 4?