0% found this document useful (0 votes)
45 views2 pages

Dynamic Memory Allocation in C

This document discusses dynamic memory allocation in C using functions like malloc(), calloc(), free(), and realloc(). It explains that C allows changing the size of data structures like arrays at runtime using these functions, which allocate and free memory as needed. Examples are provided to demonstrate reallocating memory to add elements to an array.

Uploaded by

PRAVEENAT CSE
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)
45 views2 pages

Dynamic Memory Allocation in C

This document discusses dynamic memory allocation in C using functions like malloc(), calloc(), free(), and realloc(). It explains that C allows changing the size of data structures like arrays at runtime using these functions, which allocate and free memory as needed. Examples are provided to demonstrate reallocating memory to add elements to an array.

Uploaded by

PRAVEENAT CSE
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/ 2

04/04/2024, 14:34 Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc() - GeeksforGeeks

Dynamic Memory Allocation in C using malloc(),


calloc(), free() and realloc()
Since C is a structured language, it has some fixed rules for programming.
One of them includes changing the size of an array. An array is a collection
of items stored at contiguous memory locations.

As can be seen, the length (size) of the array above is 9. But what if there is
a requirement to change this length (size)? For example,

If there is a situation where only 5 elements are needed to be entered in


this array. In this case, the remaining 4 indices are just wasting memory in
this array. So there is a requirement to lessen the length (size) of the array
from 9 to 5.
Take another situation. In this, there is an array of 9 elements with all 9
indices filled. But there is a need to enter 3 more elements in this array. In
this case, 3 indices more are required. So the length (size) of the array
needs to be changed from 9 to 12.

This procedure is referred to as Dynamic Memory Allocation in C.


Therefore, C Dynamic Memory Allocation can be defined as a procedure in
which the size of a data structure (like Array) is changed during the runtime.
C provides some functions to achieve these tasks. There are 4 library
We use cookies to ensure you have the best browsing experience on our website. By using
functions provided by C defined under <stdlib.h> header file to facilitate
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Got It !
dynamic memory allocation Policyin C programming. They are:

https://www.geeksforgeeks.org/dynamic-memory-allocation-in-c-using-malloc-calloc-free-and-realloc/ 1/16
04/04/2024, 14:34 Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc() - GeeksforGeeks

(index + 1)
* sizeof(
int)); // Dynamically reallocate
// memory by using realloc
// check if the memory is successfully
// allocated by realloc or not?
if (marks == NULL) {
printf("memory cannot be allocated");
}
else {
printf("Memory has been successfully "
"reallocated using realloc:\n");
printf(
"\n base address of marks are:%pc",
marks); ////print the base or
///beginning address of
///allocated memory
}
}
} while (ans == 1);
// print the marks of the students
for (i = 0; i <= index; i++) {
printf("marks of students %d are: %d\n ", i,
marks[i]);
}
free(marks);
}
return 0;
}

Output:

We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy

https://www.geeksforgeeks.org/dynamic-memory-allocation-in-c-using-malloc-calloc-free-and-realloc/ 12/16

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy