2

I'm learning C now and trying to figure out how the memory management of C works. Please correct me if I am wrong, but as I know for:

Static memory allocation - this happens during compile time. The compiler allocates the necessary memory needed for static memory.

Static memory deallocation - the memory is deallocated automatically when the block/function is finished running (for local variables) or when the entire program has finished executing (for global variables).

Dynamic memory allocation - the memory is allocated during run-time because the size of the input is unknown at this time.

Dynamics memory deallocation - the memory is deallocated when the free() is executed.

Is this about right? Am I missing anything?

  • Give a look here: https://stackoverflow.com/questions/8385322/difference-between-static-memory-allocation-and-dynamic-memory-allocation – ieio Jan 10 '20 at 08:08

2 Answers2

5

There are 3 different kinds of storage duration in C language:

  • static: the lifetime of the variable is the lifetime of the program. It is allocated at load time (only defined at compile time) and only freed when the operation system unloads the program. Static variables are variables declared outside any functions, and local variables (declared in a function or block) having the static modifier
  • automatic: automatic variables are declared inside a block (or function), with no storage modifier. Their lifetime starts at the beginning of the bloc and ends at the end of the bloc. They are generally allocated at the beginning of the bloc and deallocated at its end, but because of the as if rule, optimizing compilers could allocate them sooner and free them later, for example if the bloc in located inside a loop.
  • dynamic: they are allocated manually through malloc, and will only be deallocated by free

Common implementations use a system stack for automatic variables and a memory pool (asking the Operating System when more memory is needed) for dynamic ones, but this is an implementation details

When multithreading is used, a fourth kind of storage duration is available: thread storage duration. Those variable are declared with the _Thread_local storage class modifier. Their lifetime is the duration of the thread, and each thread has its own copy of them.

For common implementation, they are managed the same as static variables: they are allocated by the operating system when the thread is created, and reclaimed (still by OS) when the thread ends.


Some remarks regarding your wordings:

Static memory allocation - this happens during compile time.

Beware, compile time and load time are different. At build time only a file is created, and memory is only allocated by the system at run time

Static memory deallocation - the memory is deallocated automatically when the block/function is finished running (for local variables)...

There is a confusion between scope (local vs. global) and storage duration. A function can contain static variables, that is one of the reasons for the static keyword

Dynamic memory allocation - the memory is allocated during run-time because the size of the input is unknown at this time

This is one possible reason for the programmer to use dynamic memory, but there might be others, for example because the code would be cleaner that way. In particular, dynamic memory is a nice tool when you want to mimic Object Oriented Programming in C language.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • Does `VLA`s are dynamic or static? – TruthSeeker Jan 10 '20 at 09:57
  • @TruthSeeker It's not defined by the standard how they are allocated, but most likely they are automatic and allocated on the stack. I've never seen an implementation that did anything else. – Lundin Jan 10 '20 at 10:23
  • @TruthSeeker. They are *by definition* automatic variables, because for arrays *If an identifier is declared to be an object with static or thread storage duration, it shall not have a variable length array type.* (6.7.6.2 Array declarators §2). And malloc cannot declare VLA. – Serge Ballesta Jan 10 '20 at 10:56
-1

I think most of the words you say is correct. Just a few points I wanted to add.

For global and static variables, if they are initialized, their values are present in the resulting binary so yes, static memory allocation (actually it is not memory but anyways) happens in compile time but consider uninitialized global variables (bss section). only their length is written in the resulting binary image because writing thousands of zeros to the compiled image would be silly. in this case memory allocation is handled by loader at load time. it allocates required space, maps them to virtual addresses of your variables and zero out the memory.

And free is not necessarily meaning that you give the unused memory to the operating system. Usually the c standard library keeps track of free'd chunks, and concatenates them if it can in order to not execute a sbrk or equivalent system call next time you want to malloc because they are relatively costly. It is I believe highly dependent to the library implementation