Dynamic Memory Allocation in C
Dynamic Memory Allocation in C
First, we should discuss the little bit about the static memory allocation. Static memory allocation means fixed size. Ex: int x;
All primitive data types take static memory allocation.
All primitive data types take static memory allocation.
Array and structure also are static variable int arr[5];
structure are user define data types. Ex: struct student s[5];
fixed number of record can be stored in the above structure. Memory size is fixed. So we call it static memory allocation.
DYNAMIC MEMORY ALLOCATION
Dynamic memory allocation means storage/memory can be allocated to variables during the runtime is called
Dynamic Memory Allocation.
The number of elements in array increase and decrease in Dynamic memory allocation.
For dynamic memory allocation we have 4 library fuction define under <stdlib.h> as follows --
malloc() function use to allocate memomy to array. Representation of malloc function in c.
prototype of malloc:-
void* malloc(size_t size)
void* is a generic pointer which can hold any type of pointer variable.
size_t is unsign( +ve integer value).
If memory allocation is success the malloc return base address of memory block. On failure, it returns a NULL pointer.
ALLOCATION OF MEMORY TO STRUCTURE USING MALLOC
struct Emp
{
int eno;
char ename[20];
float salary;
};
struct Emp* ptr; //pointer type of variable ptr
ptr=(struct emp*)malloc(sizeof(struct Emp));
let consider integer take 4 byte. float 4 byte.
malloc return base address in void* pointer. we store base address of structure in ptr variable but ptr variabletype is struct Emp so type cast malloc to struct Emp type.
Example:
#include<stdio.h>
#include<stdlib.h>
struct Emp
structure are user define data types. Ex: struct student s[5];
fixed number of record can be stored in the above structure. Memory size is fixed. So we call it static memory allocation.
DYNAMIC MEMORY ALLOCATION
Dynamic memory allocation means storage/memory can be allocated to variables during the runtime is called
Dynamic Memory Allocation.
The number of elements in array increase and decrease in Dynamic memory allocation.
For dynamic memory allocation we have 4 library fuction define under <stdlib.h> as follows --
- malloc() -- used to allocate memory to structure.
- calloc() -- used to allocate memory to array.
- realloc() -- used to increase or decrease the size of array.
- free() -- used to release/delete the memory.
malloc() function use to allocate memomy to array. Representation of malloc function in c.
prototype of malloc:-
void* malloc(size_t size)
void* is a generic pointer which can hold any type of pointer variable.
size_t is unsign( +ve integer value).
If memory allocation is success the malloc return base address of memory block. On failure, it returns a NULL pointer.
ALLOCATION OF MEMORY TO STRUCTURE USING MALLOC
struct Emp
{
int eno;
char ename[20];
float salary;
};
struct Emp* ptr; //pointer type of variable ptr
ptr=(struct emp*)malloc(sizeof(struct Emp));
let consider integer take 4 byte. float 4 byte.
malloc return base address in void* pointer. we store base address of structure in ptr variable but ptr variabletype is struct Emp so type cast malloc to struct Emp type.
Example:
#include<stdio.h>
#include<stdlib.h>
struct Emp
{
int eno;
char ename[20];
float salary;
};
char ename[20];
float salary;
};
void main()
{struct Emp* ptr;
ptr=(struct Emp*)malloc(sizeof(struct Emp));
if(ptr==NULL)
{
printf("Out of Memory");
}
else
{
printf("Enter Employee detail\n");
scanf("%d %s %f ",&ptr->eno, ptr->ename, &ptr-> salary);
printf("Employee detail are: \n");
printf("%d %s %f ",ptr->eno, ptr->ename, ptr-> salary);
}
}
Comments
Post a Comment