0% found this document useful (0 votes)
10 views21 pages

12.C_DYNAMIC_MEMORY_ALLOCATION

The document explains static and dynamic memory allocation in C programming, highlighting the limitations of static allocation and the advantages of dynamic allocation. It details four built-in functions for dynamic memory allocation: malloc(), calloc(), realloc(), and free(), along with their purposes and syntax. Example programs demonstrate the usage of these functions for allocating, initializing, and freeing memory.

Uploaded by

nirmalsuresh90
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)
10 views21 pages

12.C_DYNAMIC_MEMORY_ALLOCATION

The document explains static and dynamic memory allocation in C programming, highlighting the limitations of static allocation and the advantages of dynamic allocation. It details four built-in functions for dynamic memory allocation: malloc(), calloc(), realloc(), and free(), along with their purposes and syntax. Example programs demonstrate the usage of these functions for allocating, initializing, and freeing memory.

Uploaded by

nirmalsuresh90
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/ 21

DYNAMIC MEMORY ALLOCATION

MEMORY ALLOCATION

MEMORY
ALLOCATION

STATIC DYNAMIC
MEMORY MEMORY
ALLOCATION ALLOCATION
STATIC MEMORY ALLOCATION
• array’s were used to allocate a fixed size of memory for our data.

• This size can’t be increased or decreased while execution of the program.

• change it even if the size allocated is more or less than our requirement.

• This type of allocation of memory is called Static Memory Allocation.

• This leads to wastage or shortage of memory.


DYNAMIC MEMORY ALLOCATION

• Fortunately, C allows programmer to allocate memory dynamically .


i.e. during run time and this process is called dynamic memory
allocation.

• By allocating memory dynamically, Required amount memory only


allocated.

• For this, C has four built in functions under “stdlib.h” header files for
allocating memory dynamically.
STATIC AND DYNAMIC MEMORY
PRE-DEFINED FUNCTIONS
4 TYPES OF DMA FUNCTIONS
Function Purpose
Allocates the memory of requested
• 4 Types of DMA Functionssize and returns the pointer to the
malloc()
• 4 Types of DMA Functionsfirst byte of
allocated space.
Allocates the space for elements of
an array. Initializes the elements to
calloc()
zero and returns a pointer to the
memory.

It is used to modify the size of


realloc()
previously allocated memory space.

Frees or empties the previously


Free()
allocated memory space.
malloc()[MEMORY ALLOCATION]
• malloc () function is used to allocate space in memory during the execution of the
program.

• malloc () does not initialize the memory allocated during execution. It carries
garbage value.

• malloc () function returns null pointer if it couldn’t able to allocate requested


amount of memory.

The syntax of malloc() function:


ptr=(cast-type*)malloc(byte-size)
STACK AND HEAP
EXAMPLE PROGRAM

#include <stdio.h>
#include <stdlib.h>
int main()
{ int* ptr ,n , i;
printf("Enter number of elements:);
scanf("%d",&n);
ptr = (int*)malloc(n*sizeof(int));

// Dynamically allocate memory using malloc()

if (ptr == NULL)
{
printf("Memory not allocated.\n");
exit(0);
}
else
{
printf("Memory successfully allocated using malloc.\n");
for (i = 0; i < n; ++i)
ptr[i] = i + 1;

printf("The elements of the array are:"); // Print the elements of the array
for (i = 0; i < n; ++i)
printf("%d, ", ptr[i]);
}

free(ptr);
ptr=NULL;
return 0;
}
Calloc()[CONTIGUOUS ALLOCATION]
• calloc () function is also like malloc () function.

• But calloc () initializes the allocated memory to zero.

• But, malloc() doesn’t. It has two parameters or arguments as compare to malloc().

• It returns NULL if memory is not sufficient.

The syntax of calloc() function:

ptr=(cast-type*)calloc(number, byte-size)
Calloc()
EXAMPLE PROGRAM

#include <stdio.h>
#include <stdlib.h>
int main()
{ int* ptr ,n , i;
printf("Enter number of elements:);
scanf("%d",&n);

ptr = (int*)calloc(n, sizeof(int));

// Dynamically allocate memory using calloc()

if (ptr == NULL)
{
printf("Memory not allocated.\n");
exit(0);
}
else
{
printf("Memory successfully allocated using calloc.\n");
for (i = 0; i < n; ++i)
ptr[i] = i + 1;

printf("The elements of the array are:"); // Print the elements of the array
for (i = 0; i < n; ++i)
printf("%d, ", ptr[i]);
}
free(ptr);
ptr=NULL;
return 0;
}
free() FUNCTION
• “free” method in C is used to dynamically de-allocate the memory.

• The memory allocated using functions malloc() ,calloc() and realloc () is not de-
allocated on their own.

• Hence the free() method is used, whenever the dynamic memory allocation takes
place.

• It helps to reduce wastage of memory by freeing it.

Syntax of free():
free(ptr);
realloc () [RE-ALLOCATION]

• 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() function:


ptr=realloc(ptr, new-size)
Realloc()
EXAMPLE PROGRAM

#include <stdio.h>
#include <stdlib.h>
int main()
{
int* ptr ,n1 ,n2, i;
printf("Enter size:);
scanf("%d",&n1);

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

printf("Addresses of previously allocated Memory.\n");

for (i = 0; i < n1; ++i)


printf("%u\n", ptr+i);
printf("Enter new size:\n);
scanf("%d",&n2);
ptr = realloc(ptr,n2*sizeof(int)); // reallocating the memory

printf("Addresses of newly allocated memory are:");

for (i = 0; i < n2; ++i)

printf("%u\n ", ptr+i);

free(ptr);

ptr=NULL;

return 0;
}
THANK YOU
ANY QUERIES??

You might also like