The Joy of Programming
Allocation of Local Variables in Stack
It is always fun to understand the low-level details of a program and to explore how compilers
work.
In this column, we’ll see some details on how local variables are allocated in stack and
some...
More
The Joy of Programming
Allocation of Local Variables in Stack
It is always fun to understand the low-level details of a program and to explore how compilers
work.
In this column, we’ll see some details on how local variables are allocated in stack and
some interesting issues on how compilers handle it.
Every month, I get lots of mails and queries from LFY readers.
This column seems to have become
popular, particularly among students.
This month, we’ll cover an interesting question from
Saravanan swamy (GCT, Coimbatore).
His question is: Why does the following program print 10?
int i = 10;
printf(“%d”);
Note that the printf doesn’t obviously have i as matching argument for the format string “%d”.
So, the program is incorrect and you can reasonably expect the program to print some garbage
value.
But note that it is equally possible that you might get 10 as output.
Why? To understand
this, let us see how compiler might transform these statements to low-level executable code.
Loca
Less