Static Memory and Dynamic Memory
This “Static Memory and Dynamic Memory” post explains clearly what is the difference between static memory and dynamic memory and where we use static memory allocation and dynamic memory allocation.
In C-language, we can allocate the memory to variables in 2 ways :
- Static memory allocation
- Dynamic memory allocation
Primitive data type : All the primitive variables always get static memory allocation
int a, b ;
printf(“enter 2 integers….);
scanf(“%d%d”, &a, &b);
printf(“Elements are : %d , %d \n”, a , b);
- I compiled this program in one computer (.exe file will be generated).
- I am executing this program in another computer.
- Static memory is not compile time memory allocation.
- Static memory is the fixed size of memory that will be created as soon as you run the application.
- We can’t increase and decrease the static memory.
- We can allocate the memory to array variable in two ways.
- Generally when we declare array variable, we can fix the size.
- We can’t store elements beyond its size
- If we want to allocate memory dynamically(size is not fixed), we use calloc() functions.
- Using pointers, we can allocate memory dynamically to Array.
- We can allocate the memory to structures in two ways.
- Generally when we declare structure variable, the size will be fixed to store elements of structure.
- We can’t store structure records beyond size.
- for example struct Employee e, variable can store maximum of 3 records.
- If we want to allocate memory dynamically(size is not fixed), we use malloc() functions.
- Using pointers, we can allocate memory dynamically to Structure.