Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between static memory allocation and dynamic memory allocation

I would like to know what is the difference between static memory allocation and dynamic memory allocation?

Could you explain this with any example?

like image 289
Nishant Kumar Avatar asked Dec 05 '11 12:12

Nishant Kumar


People also ask

What is the difference between static memory allocation and dynamic memory allocation?

Static Memory Allocation is done before program execution. Dynamic Memory Allocation is done during program execution. In static memory allocation, once the memory is allocated, the memory size can not change. In dynamic memory allocation, when memory is allocated the memory size can be changed.

What is the difference between static and dynamic stacks?

Size of static storage area is constant throughout execution but dynamic stack grows and shrinks as per push and pop of activation record.

What is static memory allocation?

The static memory allocation is a fixed amount of memory that is allocated during the compile time of a program and the stack data structure. There are different types of memory architectures available in C language and memory is allocated in two areas, either in the stack memory area or the heap memory area.

What is static & dynamic allocation in C ++?

In static memory allocation, memory is allocated before the execution of the program begins. In Dynamic memory allocation, memory is allocated during the execution of the program.


1 Answers

This is a standard interview question:

Dynamic memory allocation

Is memory allocated at runtime using calloc(), malloc() and friends. It is sometimes also referred to as 'heap' memory, although it has nothing to do with the heap data-structure ref.

int * a = malloc(sizeof(int)); 

Heap memory is persistent until free() is called. In other words, you control the lifetime of the variable.

Automatic memory allocation

This is what is commonly known as 'stack' memory, and is allocated when you enter a new scope (usually when a new function is pushed on the call stack). Once you move out of the scope, the values of automatic memory addresses are undefined, and it is an error to access them.

int a = 43; 

Note that scope does not necessarily mean function. Scopes can nest within a function, and the variable will be in-scope only within the block in which it was declared. Note also that where this memory is allocated is not specified. (On a sane system it will be on the stack, or registers for optimisation)

Static memory allocation

Is allocated at compile time*, and the lifetime of a variable in static memory is the lifetime of the program.

In C, static memory can be allocated using the static keyword. The scope is the compilation unit only.

Things get more interesting when the extern keyword is considered. When an extern variable is defined the compiler allocates memory for it. When an extern variable is declared, the compiler requires that the variable be defined elsewhere. Failure to declare/define extern variables will cause linking problems, while failure to declare/define static variables will cause compilation problems.

in file scope, the static keyword is optional (outside of a function):

int a = 32; 

But not in function scope (inside of a function):

static int a = 32; 

Technically, extern and static are two separate classes of variables in C.

extern int a; /* Declaration */ int a; /* Definition */ 

*Notes on static memory allocation

It's somewhat confusing to say that static memory is allocated at compile time, especially if we start considering that the compilation machine and the host machine might not be the same or might not even be on the same architecture.

It may be better to think that the allocation of static memory is handled by the compiler rather than allocated at compile time.

For example the compiler may create a large data section in the compiled binary and when the program is loaded in memory, the address within the data segment of the program will be used as the location of the allocated memory. This has the marked disadvantage of making the compiled binary very large if uses a lot of static memory. It's possible to write a multi-gigabytes binary generated from less than half a dozen lines of code. Another option is for the compiler to inject initialisation code that will allocate memory in some other way before the program is executed. This code will vary according to the target platform and OS. In practice, modern compilers use heuristics to decide which of these options to use. You can try this out yourself by writing a small C program that allocates a large static array of either 10k, 1m, 10m, 100m, 1G or 10G items. For many compilers, the binary size will keep growing linearly with the size of the array, and past a certain point, it will shrink again as the compiler uses another allocation strategy.

Register Memory

The last memory class are 'register' variables. As expected, register variables should be allocated on a CPU's register, but the decision is actually left to the compiler. You may not turn a register variable into a reference by using address-of.

register int meaning = 42; printf("%p\n",&meaning); /* this is wrong and will fail at compile time. */ 

Most modern compilers are smarter than you at picking which variables should be put in registers :)

References:

  • The libc manual
  • K&R's The C programming language, Appendix A, Section 4.1, "Storage Class". (PDF)
  • C11 standard, section 5.1.2, 6.2.2.3
  • Wikipedia also has good pages on Static Memory allocation, Dynamic Memory Allocation and Automatic memory allocation
  • The C Dynamic Memory Allocation page on Wikipedia
  • This Memory Management Reference has more details on the underlying implementations for dynamic allocators.
like image 94
brice Avatar answered Oct 22 '22 13:10

brice