0% found this document useful (0 votes)
56 views20 pages

Dynamic Memory Allocation Jhalak

The document discusses dynamic memory allocation in C using functions like malloc(), calloc(), realloc(), and free(). It explains that dynamic allocation allows memory to be allocated at runtime unlike static allocation. The functions malloc() allocates a single block, calloc() allocates multiple blocks and initializes to 0, realloc() changes the size of allocated memory, and free() releases the memory. Examples of using each function are provided.

Uploaded by

BlaZe D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views20 pages

Dynamic Memory Allocation Jhalak

The document discusses dynamic memory allocation in C using functions like malloc(), calloc(), realloc(), and free(). It explains that dynamic allocation allows memory to be allocated at runtime unlike static allocation. The functions malloc() allocates a single block, calloc() allocates multiple blocks and initializes to 0, realloc() changes the size of allocated memory, and free() releases the memory. Examples of using each function are provided.

Uploaded by

BlaZe D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Dynamic Memory

Allocation

Prof. Jhalak Dutta


Heritage Institute of Technology
Department of Computer Science and Engineering
The concept of dynamic memory
allocation in c language enables the C
programmer to allocate memory at
runtime. Dynamic memory allocation in c
language is possible by 4 functions of
stdlib.h header file.
1.malloc()
2.calloc()
3.realloc()
4.free()
Static vs. Dynamic Memory
Allocation
Static Memory Dynamic Memory
Allocation Allocation
memory is allocated memory is allocated
at compile time. at run time.
memory can't be memory can be
increased while increased while
executing program. executing program.
used in array. used in linked list.
malloc() allocates single block of
requested memory.
calloc() allocates multiple block of
requested memory.
realloc() reallocates the memory
occupied by malloc() or
calloc() functions.
free() frees the dynamically
allocated memory.
MALLOC() FUNCTION IN C
•The malloc() function allocates single
block of requested memory.
•It doesn't initialize memory at execution
time, so it has garbage value initially.
•It returns NULL if memory is not
sufficient.
Syntax of malloc()
ptr = (cast-type*) malloc(byte-size)
Here, ptr is pointer of cast-type. The malloc()
function returns a pointer to an area of memory
with size of byte size. If the space is insufficient,
allocation fails and returns NULL pointer.

ptr = (int*) malloc(100 * sizeof(int));


This statement will allocate either 200 or 400
according to size of int 2 or 4 bytes respectively
and the pointer points to the address of first byte
of memory.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int));
for(i=0;i<n;i++)
{
printf("%d \n", ptr[i]);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int));
for(i=0;i<n;i++)
{
ptr[i]=i+1;
}
for(i=0;i<n;i++)
{
printf("%d \n", ptr[i]);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
void main()
{
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int));
if(ptr==NULL)
{
printf("Sorry! unable to allocate memory");
exit(0);
}
printf("Enter elements of array: ");
for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
sum=sum+*(ptr+i);
}
printf("Sum=%d",sum);
free(ptr);
}
Write a C program to find sum of n elements entered by user. To perform this program,
allocate memory dynamically using malloc() function.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &num);
ptr = (int*) malloc(num * sizeof(int));
if(ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements of array: ");
for(i = 0; i < num; i++)
{
scanf("%d", ptr + i);
sum = sum+ *(ptr + i);
}
printf("Sum = %d", sum);
free(ptr);
return 0;
}
CALLOC() FUNCTION IN C
calloc () function is also like malloc ()
function. But calloc () initializes the allocated
memory to zero. But, malloc() doesn’t.
•The calloc() function allocates multiple
block of requested memory.

•It initially initialize all bytes to zero.

•It returns NULL if memory is not


sufficient.
Syntax of calloc()
ptr = (cast-type*)calloc(n, element-size);
This statement will allocate contiguous space in
memory for an array of n elements. For example:

ptr = (float*) calloc(25, sizeof(float));


This statement allocates contiguous space in
memory for an array of 25 elements each of size
of float, i.e, 4 bytes.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)calloc(n,sizeof(int));
for(i=0;i<n;i++)
{
printf("%d \n", ptr[i]);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)calloc(n,sizeof(int));
for(i=0;i<n;i++)
{
ptr[i]=i+1;
}
for(i=0;i<n;i++)
{
printf("%d \n", ptr[i]);
}
return 0;
}
malloc calloc
The name malloc stands for memory allocation. The name calloc stands for contiguous
allocation.
void *malloc(size_t n) returns a pointer void *calloc(size_t n, size_t size) returns a
to n bytes of uninitialized storage, or NULL if pointer to enough free space for an array
the request cannot be satisfied. If the space of n objects of the specified size, or NULL if the
assigned by malloc() is overrun, the results are request cannot be satisfied. The storage is
undefined. initialized to zero.

malloc() takes one argument that is, number of calloc() take two arguments those are: number
bytes. of blocks and size of each block.
syntax of malloc(): syntax of calloc():
void *malloc(size_t n); void *calloc(size_t n, size_t size);
Allocates n bytes of memory. If the allocation Allocates a contiguous block of memory large
succeeds, a void pointer to the allocated enough to hold n elements of size bytes each.
memory is returned. Otherwise NULL is The allocated region is initialized to zero.
returned.
malloc is faster than calloc. calloc takes little longer than malloc because of
the extra step of initializing the allocated
memory by zero. However, in practice the
difference in speed is very tiny and not
recognizable.
Write a C program to find sum of n elements entered by user. To perform this
program, allocate memory dynamically using calloc() function.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &num);
ptr = (int*) calloc(num, sizeof(int));
if(ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements of array: ");
for(i = 0; i < num; i++)
{
scanf("%d", ptr + i);
sum = sum + *(ptr + i);
}
printf("Sum = %d", sum);
free(ptr);
return 0;
}
REALLOC() FUNCTION IN C
•realloc () function modifies the allocated memory
size by malloc () and calloc () functions to new
size.
•If enough space doesn’t exist in memory of
current block to extend, new block is allocated for
the full size of reallocation, then copies the
existing data to new block and then frees the old
block.
Syntax of realloc()
ptr = realloc(ptr, newsize);
Here, ptr is reallocated with size of new size.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr, i , n1, n2;
printf("Enter size of array: ");
scanf("%d", &n1);
ptr = (int*) malloc(n1 * sizeof(int));
printf("Address of previously allocated memory: ");
for(i = 0; i < n1; i++)
{
printf("%u\t",ptr + i);
}
printf("\nEnter new size of array: ");
scanf("%d", &n2);
ptr = realloc(ptr, n2);
for(i = 0; i < n2; i++)
{
printf("%u\t", ptr + i);
}
return 0;
}
FREE() FUNCTION IN C

•free () function frees the allocated


memory by malloc (), calloc (), realloc ()
functions and returns the memory to the
system.
syntax of free()
free(ptr);
This statement frees the space
allocated in the memory pointed by ptr.

You might also like