2
Reply

Variables on the stack and their mapping???

vikrampschauhan

vikrampschauhan

Dec 13 2004 7:18 PM
1.9k
In the function - void main() { int i=10; int j=20; int sum=i+j; return; } Following happens - 1. push(int); - the sp is incremented by int bytes. This space(bytes){b/w sp's previous value and incremented value} is marked as i. 10 is put in this space. 2. push(int); - the sp is incremented by int bytes again. This space(bytes) is marked as j. 20 is put in this space. 3. push(int); - the sp is incremented by int bytes again. This space(bytes) is marked as sum. 30 is put in this space. When the function returns, everything is popped from the stack in the reverse sequence: 1. pop(int) ; - sp is decremented by int bytes. Now sp points to the begining of sum(30) and sum can be overwritten. 1. pop(int) ; - sp is decremented by int bytes again. Now sp points to the begining of j(20) and j can be overwritten. 1. pop(int) ; - sp is decremented by int bytes again. Now sp points to the begining of i(10) and i can be overwritten. Now, there must also be a table that maps the variables to their corresponding address: For eg a table containing the following entry: i - 0xAAAA (ie starting address of i) j - 0xBBBB (ie starting address of j) sum - 0xCCCC (ie starting address of sum) so that when we refer i, we know what address are we talking about(eg when we say i=10;) What is this table and at what point is it destroyed? Thanks Vikram

Answers (2)