Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Same memory space being allocated again & again

Tags:

c

linux

unix

gcc

In each loop iteration, variable j is declared again and again. Then why is its address remaining same?

  • Shouldn't it be given some random address each time?
  • Is this compiler dependent?
#include<stdio.h>
#include<malloc.h>

int main()
{
    int i=3;
    while (i--)
    {
        int j;
        printf("%p\n", &j);
    }
    return 0;
}

Testrun:-

shadyabhi@shadyabhi-desktop:~/c$ gcc test.c
shadyabhi@shadyabhi-desktop:~/c$ ./a.out
0x7fffc0b8e138
0x7fffc0b8e138
0x7fffc0b8e138
shadyabhi@shadyabhi-desktop:~/c$
like image 244
Abhijeet Rastogi Avatar asked Mar 15 '10 14:03

Abhijeet Rastogi


2 Answers

It is memory on the stack. It is not allocated from the heap. The stack would not change in that loop.

like image 114
Mark Wilkins Avatar answered Oct 18 '22 18:10

Mark Wilkins


The reason why the address of j never changes is because the compiler allocates memory for j on the stack when the function is entered as opposed to when j comes into scope.

As always, looking at some assembly code might help explain the concept. Take the following function:-

int foo(void)
   {
   int i=3;
   i++;
      {
      int j=2;
      i=j;
      }
   return i;
   }

gcc converts this to the following x86 assembly code:-

foo:
    pushl   %ebp                 ; save stack base pointer
    movl    %esp, %ebp           ; set base pointer to old top of stack
    subl    $8, %esp             ; allocate memory for local variables
    movl    $3, -4(%ebp)         ; initialize i
    leal    -4(%ebp), %eax       ; move address of i into eax
    incl    (%eax)               ; increment i by 1
    movl    $2, -8(%ebp)         ; initialize j
    movl    -8(%ebp), %eax       ; move j into accumulator
    movl    %eax, -4(%ebp)       ; set i to j
    movl    -4(%ebp), %eax       ; set the value of i as the function return value
    leave                        ; restore stack pointers
    ret                          ; return to caller

Let's walk through this assembly code. The first line saves the current stack base pointer so that it can be restored when the function exits, the second line sets the current top of the stack to be the new stack base pointer for this function.

The third line is the one that allocates the memory on the stack for all the local variables. The instruction subl $8, %esp subtracts 8 from the current top of the stack pointer, the esp register. Stacks grow down in memory so this line of code actually increases the memory on the stack by 8 bytes. We have two integers in this function, i and j, each of which require 4 bytes, hence why it allocates 8 bytes.

Line 4 initializes i to 3 by directly writing to an address on the stack. Lines 5 and 6 then load and increment i. Line 7 initializes j by writing the value 2 into the memory allocated for j on the stack. Note that when j came into scope at line 7 the assembly code did not adjust the stack to allocate memory for it, that had already been taken care of earlier.

I'm sure it's obvious, but the reason why the compiler allocates the memory for all the local variables at the start of the function is because it is way more efficient to do so. Adjusting the stack each time a local variable went in or out of scope would result in a lot of unnecessary manipulations of the stack pointer for no gain.

I'm sure you can work out what the rest of the assembly code does yourself, if not post a comment and I'll walk you through it.

like image 26
Andrew O'Reilly Avatar answered Oct 18 '22 17:10

Andrew O'Reilly