General Discussions

Static V/S Dynamic Memory Allocation


Si. No Static Memory Allocation Dynamic Memory Allocation
1 Memory allocation happens at compile time Memory allocation happens at Run time
2 It applies to global variables, file scope variables, and variables qualified with static defined inside functions It applies to variables that allocate memory during runtime using memory allocation API’s
3 The size is fixed when the program is created Programmer can control the exact size and the lifetime of the memory locations
4 Memory allocated at compile time in stack or other data segments Memory is allocated during run-time in heap
5. This is used when the size of memory is static/constant and is known during compile-time This is used when the size of memory is variable and is known only during run-time
6. The compiler allocates the required memory space for a declared variable It uses functions such as malloc( ) or calloc( ) to get memory dynamically
7. Memory is allocated before the execution of the program begins Memory is allocated during the execution of the program
8. Static allocation will be much faster. Static allocation can happen at global scope, and on the stack.

So, Faster execution than Dynamic

Dynamic memory must be allocated from a heap, and even in the best case most allocations will take time that scales more than linear with each allocation.

So, Slower execution than static

9. More memory Space required Less Memory space required

One thought on “Static V/S Dynamic Memory Allocation

Let me Know What you Think!

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s