4

When does memory gets allocated for a variable in c? Does it happen during declaration or initialization? Does this differ based on the scope or storage class?

Eg:

int i; <<<<<<<< memory gets allocated here?
i=10;  <<<<<<<< memory gets allocated here?

I think, it gets allocated during declaration itself.Correct me if I am wrong.

5
  • 6
    in short: depends. share your case. Commented Apr 17, 2015 at 7:39
  • 1
    For plain variables, the compiler handles it at compilation time. Commented Apr 17, 2015 at 7:43
  • @GrijeshChauhan not at int i: if block scope, at the entry of the block i is declared, see my answer.
    – ouah
    Commented Apr 17, 2015 at 7:59
  • @ouah yes I have seen your answer, and I am unsure if memory not allocated at int i. Edit ok got your point, you are correct thanks Commented Apr 17, 2015 at 8:04
  • 2
    Auto variables are typically allocated at the previous { symbol
    – Bregalad
    Commented Apr 17, 2015 at 8:12

3 Answers 3

7
  • Local function variables are allocated on the stack frame and initialized when you call the function.
  • Arguments passed to functions are either on the stack or passed through registers. This depends on your calling convention.
  • They can be allocated on the heap, if you use malloc and friends.
  • The static variables are allocated in the data section if they have initialization values (static int a=1;), otherwise they will implicitly be zeroed out and allocated in the BSS section (static int a;). They are initialized before calling main.

As for your specific example,

int i;
i = 10;

the compiler will allocate i on the stack frame. It will probably set the value right away. So it will allocate and initialize it when entering that scope.

Take for instance

#include <stdio.h>

int main()
{
  int foo;
  foo = 123;
  printf("%d\n", foo);
}

Now compile this with

gcc -O0 a.c -S

This produces the assembly file a.s. If you inspect it, you will indeed see that foo is copied right on the stack frame:

movl    $123, -4(%rbp)

or, in Intel syntax (add -masm=intel to gcc):

mov     DWORD PTR [rbp-4], 123

Right below that you will see a call printf. The RBP register refers to the stack frame, so this variable in this case only ever exists on the stack frame, because it's only used in the call to printf.

5

Memory can be allocated:

  • In one of the program's data segments by the compiler. These segments are part of the program loaded by the OS when the program starts (or paged in as needed). (Static variables)
  • On the stack at runtime. (Stack/auto variables)
  • From the heap at runtime. (Via malloc() or something similar)
4
int bla = 12;  // bla is allocated prior to program startup

int foo(void)
{
    while (1)
    {
        /* ... code here ... */
        int plop = 42;  // plop is allocated at the entry of the while block
        static int tada = 271828; // tada is allocated prior to program startup
    }

    int *p = malloc(sizeof *p);  // object is allocated after malloc returns
}
3
  • Thanks for ur answer. but i wanted to know whether memory gets allocated during declaration or initialization of a variable?
    – Karthick
    Commented Apr 17, 2015 at 8:02
  • I would like to break declaration and assigned for OP, I tried your code Commented Apr 17, 2015 at 8:03
  • @Karthick in your example int i; the i = 10; is not an initialization but an assignment statetement. At block scope, the i object is allocated when the { where it is declared opens.
    – ouah
    Commented Apr 17, 2015 at 8:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.