1

I read in documentation of static memory allocation and dynamic memory allocation that

Static memory allocation refers to the process of reserving memory at compile-time before the associated program is executed, unlike dynamic-memory allocation which took place at run-time.

My question is:-

How memory can be reserved during compile-time?

Take for example

#include <stdlib.h>
void main()
{
    int i;
}

The above code is an example of static memory allocation.

The variable i will load into the memory only during run-time i.e. during the execution of the program, then how memory can be allocated to it during compile-time?

kevin gomes
  • 1,775
  • 5
  • 22
  • 30

5 Answers5

2

When compiling this program into assembly:

int i = 7;
int main()
{
     return 0;
}

You can see that the global variable i resides in the "data" section and is statically allocated.

    .globl _i
    .data        ## data section (for explicitly initialized global/static vars)
    .align 2
_i:              ## the global variable i 
    .long   7    ## is allocated here
    .text
    .globl _main
_main:
LFB0:
    pushl   %ebp
LCFI0:
    movl    %esp, %ebp
LCFI1:
    movl    $0, %eax
    popl    %ebp
LCFI2:
    ret
LFE0:
    .section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support
EH_frame1:
    .set L$set$0,LECIE1-LSCIE1
    .long L$set$0
LSCIE1:
    .long   0
    .byte   0x1
    .ascii "zR\0"
    .byte   0x1
    .byte   0x7c
    .byte   0x8
    .byte   0x1
    .byte   0x10
    .byte   0xc
    .byte   0x5
    .byte   0x4
    .byte   0x88
    .byte   0x1
    .align 2
LECIE1:
LSFDE1:
    .set L$set$1,LEFDE1-LASFDE1
    .long L$set$1
LASFDE1:
    .long   LASFDE1-EH_frame1
    .long   LFB0-.
    .set L$set$2,LFE0-LFB0
    .long L$set$2
    .byte   0
    .byte   0x4
    .set L$set$3,LCFI0-LFB0
    .long L$set$3
    .byte   0xe
    .byte   0x8
    .byte   0x84
    .byte   0x2
    .byte   0x4
    .set L$set$4,LCFI1-LCFI0
    .long L$set$4
    .byte   0xd
    .byte   0x4
    .byte   0x4
    .set L$set$5,LCFI2-LCFI1
    .long L$set$5
    .byte   0xc4
    .byte   0xc
    .byte   0x5
    .byte   0x4
    .align 2
LEFDE1:
    .subsections_via_symbols
jh314
  • 27,144
  • 16
  • 62
  • 82
1

Objects in C can have one of three storage durations:

  • static
  • auto
  • dynamic

Objects with static storage duration have memory allocated for them when the program starts up, and the memory won't be released until the program exits. This is usually done by reserving space within the program image itself; IOW, the binary file of your program has a few sections reserved for constant (.rdata or .rodata) and non-constant (.bss) data. That's what they mean by being reserved at compile time; the compiler sets aside sections within the generated binary file for data storage. These sections aren't usable until the program is loaded into memory and run, though.

Objects with auto storage duration have memory allocated for them at runtime when the program enters their enclosing scope and released when the program exits that scope.

Given the following code:

void foo( void )
{
  int x;
  for ( x = 0; x < 100; x++ )
  {
    int y = x * 2;
    ...
  }
}

Logically speaking, the space for x will be set aside when you enter the function foo and held until foo exits, and the space for y will be set aside when you enter the for loop and released when the loop exits. In practice (at least on the platforms I'm familiar with), the space for both will be set aside at function entry and released at function exit, but you shouldn't assume that the space for y will be usable outside of the loop.

Most systems use a stack for managing objects with auto storage duration1.

Objects with dynamic storage duration have their memory set aside by calling the library functions malloc, calloc, or realloc, and that memory is held until it is explicitly released with a call to free:

void *foo( void )
{
  void *mem = malloc( SOME_MEMORY );
  ...
  return mem;
}

void bar( void )
{
  void *data = foo();
  ...
  free( data );
}

The variables mem and bar both have auto storage duration. mem only exists within foo and data only exists within bar. However, the object they both point to has dynamic storage duration; it is allocated in foo and held until explcitly released in bar.

The memory pool for these objects is usually referred to as the "heap".


1. C was originally designed on systems that used a stack for managing runtime data, so it's a natural way to implement the behavior of auto variables. However, the language definition is written in such a way to accomodate non-stack-based systems, although they'd be a pain in the ass to implement.
John Bode
  • 119,563
  • 19
  • 122
  • 198
  • 2
    Have used non-stack-based system, was a pain in the ass. – M.M Aug 19 '14 at 20:28
  • `This is usually done by reserving space within the program image itself.` please elaborate it a bit more? – kevin gomes Aug 26 '14 at 18:15
  • @kevingomes: some sections within the generated binary file will be reserved for storing static data items; those data items will occupy the same memory segment as your program text (code), as opposed to the stack or heap, which are different segments in memory. This way, those data items will exist as soon as your program is loaded into memory (even before `main` executes), and will stick around until the program is unloaded. – John Bode Aug 26 '14 at 20:32
0

When compiling your code, the compiler decides how much memory to assign to each object (say variable or code of function), in which order they will be placed in memory and at what addresses. Of course variables might not get created until the program is started, but the memory is allocated in advance, the variable addresss is already compiled into the code - for example the i variable address is compiled into the i=7; instruction.

EDIT

Variables may be allocated statically or dynamically, and dynamic variables may be allocated automatically (in C/C++ they are usually local variables in functions, created on a stack or in registers) or 'manually', that is in controllable manner (created by malloc or new and disposed by free or delete). Static variables exist through the whole process life time, dynamic variables are created when needed and destroyed after use.

int fun( int param)
{
    int automatic_var = 3;
    return automatic_var + ++param;
}

int static_var = 7;

void main()
{
    int automatic_var2;
    automatic_var2 = fun( static_var);
}

In the code above, automatic_var2 gets assigned value 11.
Variable static_var existsts through the whole process execution time,
variable automatic_var2 exists during the main() execution time,
variable automatic_var and param exist only during the fun() execution time.

Sometimes, automatic allocation is considered a third way, separate from automatic dynamic allocation. Anyway, the int i; declaration inside a function, whether considered dynamic or not, certainly is not a static allocation (even if it is main(), whose execution time covers almost the whole proces execution time).

CiaPan
  • 9,381
  • 2
  • 21
  • 35
  • `but the memory is allocated in advance`.so does it do not occur in case of dynamic memory allocation? – kevin gomes Aug 19 '14 at 17:56
  • Opposite is answered here: http://stackoverflow.com/questions/8385322/difference-between-static-memory-allocation-and-dynamic-memory-allocation – kevin gomes Aug 19 '14 at 18:05
0

In C there are four forms of storage:

  • automatic (aka. stack)
  • thread
  • static
  • allocated (aka. heap)

Some authors divide these up into two categories, static and dynamic . Everyone agrees that static is static, and allocated is dynamic, but your example is automatic, so assigning it to one of the other categories is rather arbitrary. It seems clearer to me to just use the standard terms.

M.M
  • 138,810
  • 21
  • 208
  • 365
0

Irrespective of static and dynamic memory allocation, your program gets memory only when it is executing.

While executing there comes the concept of static and dynamic. When executing a static memory is allocated in one of data or code segment. Dynamic allocation happens when u call malloc or calloc which gets memory in heap and this happens only when the statement containing malloc or calloc happens and they are called dynamic.

It is not like after compiling the memory is allocated on ur RAM. In C static and dynamic concept comes only when the program is executing, else there is no memory allocated to your code.