“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”
Stacks always grow. If a stack is shrinking, you’ve either got a broken machine, or a time machine, in which case you probably also have a cyber-quantum-singularity-trinary-micro-cpu computer from the future, so you wouldn’t really care either way.
- Allocate two local variables and print the addresses of each one to find out where the stack is growing
ex: func()
{
int a;
int b;
printf(”0x%x, 0x%x\n”, &a, &b);
}
if a has higher memory address than b then stack is growing down else upwards
- This can be achieved using stack frame.
int main(…)
{
int a;
printf(”0x%x\n”, &a);
func();
}
You may do it with only one variable as below
int main(void) {
int a;
if (((int)(&a + 1) - (int)&a)) > 0) {
/*Stack grows upwards*/
} else {
/*Stack grows downwards*/
}
return 1;
}
Submitted By: Devendra khandelwal — December 30, 2007
Stacks always grow. If a stack is shrinking, you’ve either got a broken machine, or a time machine, in which case you probably also have a cyber-quantum-singularity-trinary-micro-cpu computer from the future, so you wouldn’t really care either way.
Print memory locations from the stack
Two possible ways
- Allocate two local variables and print the addresses of each one to find out where the stack is growing
ex: func()
{
int a;
int b;
printf(”0x%x, 0x%x\n”, &a, &b);
}
if a has higher memory address than b then stack is growing down else upwards
- This can be achieved using stack frame.
int main(…)
{
int a;
printf(”0x%x\n”, &a);
func();
}
void func()
{
int b;
printf(”0x%x\n”, &b);
}
You may do it with only one variable as below
int main(void) {
int a;
if (((int)(&a + 1) - (int)&a)) > 0) {
/*Stack grows upwards*/
} else {
/*Stack grows downwards*/
}
return 1;
}
#include
int main(void)
{
int a;
if ((((int)(&a + 1) - (int)&a)) > 0)
{
printf(”Stack grows upwards”);
}
else
{
printf(”Stack grows downwards”);
}
return 1;
}
Leave an Answer/Comment