Dynamic Memory Allocation: Example Program
Get introduced to dynamic memory allocation with the help of an interactive example.
We'll cover the following...
As we already know, in dynamic allocation, no arrangement is done during compilation. The actual allocation of memory is made during the execution, and it is done on the heap.
Example program
See the code given below!
Press + to interact
#include <stdio.h>#include <stdlib.h>int main( ){// Declare pointer variablesint *i ;float *a ;/* Allocate memory using malloc and store the startingaddress of allocated memory in pointer variable*/i = (int*) malloc (sizeof(int));a = (float*) malloc (sizeof(float));// Declare employee structurestruct emp{char name [20];int age;float sal;};// Declare structure pointerstruct emp *e;e = (struct emp *) malloc (sizeof (struct emp));}
Explanation
Lines 6,7, and ...