Difference Between Static Memory Allocation and Dynamic Memory Allocation

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?

0

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

David Miller
Author

David Miller

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.