Skip to main content
Adds more text
Source Link
csl
  • 11.2k
  • 5
  • 61
  • 90
  • 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)

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

mov     DWORD PTR [rbp-4], 123

Right below that you will see, a call printf. The theRBP register RBP 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.

  • 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)

As you see, the register RBP refers to the stack frame.

  • 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.

Answers specific question.
Source Link
csl
  • 11.2k
  • 5
  • 61
  • 90
  • 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)

As you see, the register RBP refers to the stack frame.

  • 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.
  • 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)

As you see, the register RBP refers to the stack frame.

Rewrote entire answer
Source Link
csl
  • 11.2k
  • 5
  • 61
  • 90

Short answer (and I hope I got it right):

Local variables are allocated on the stack frame and initialized when you call the function:

void foo()
{
  int a=1;
}

If you use malloc, it will be allocated on the heap.

If you use static, as in

static int a=1;

it will be allocated in the data section, if it's implicitly initialized to zero, e.g.

static int a;

it's allocated in BSS section and its contents initialized to zero.

  • 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.

Short answer (and I hope I got it right):

Local variables are allocated on the stack frame and initialized when you call the function:

void foo()
{
  int a=1;
}

If you use malloc, it will be allocated on the heap.

If you use static, as in

static int a=1;

it will be allocated in the data section, if it's implicitly initialized to zero, e.g.

static int a;

it's allocated in BSS section and its contents initialized to zero.

  • 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.
Source Link
csl
  • 11.2k
  • 5
  • 61
  • 90
Loading