0% found this document useful (0 votes)
19 views15 pages

Dynamic Memory Allocation

Dynamic memory allocation allows programs to request memory at runtime, contrasting with static allocation at compile-time. Functions like malloc, calloc, and realloc are used in languages such as C and C++ to manage memory dynamically, including allocating, resizing, and deallocating memory. The free function is essential for releasing memory once it is no longer needed to prevent memory leaks.

Uploaded by

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

Dynamic Memory Allocation

Dynamic memory allocation allows programs to request memory at runtime, contrasting with static allocation at compile-time. Functions like malloc, calloc, and realloc are used in languages such as C and C++ to manage memory dynamically, including allocating, resizing, and deallocating memory. The free function is essential for releasing memory once it is no longer needed to prevent memory leaks.

Uploaded by

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

DYNAMIC MEMORY

ALLOCATION
• Dynamic memory allocation is a concept in
computer programming that refers to the allocation
and deallocation of memory during the execution of
a program. Unlike static memory allocation, where
memory is allocated at compile-time, dynamic
memory allocation allows the program to request
memory at runtime.
• In many programming languages, dynamic memory allocation is
typically performed using functions or operators specifically designed
for this purpose, such as malloc, calloc, realloc (in C and C++), or the
new operator (in C++). These functions allow you to allocate memory
dynamically and return a pointer to the allocated memory block.
MALLOC:

• malloc: It is used to allocate a specific


amount of memory in bytes. It takes
the size of the memory block as an
argument and returns a pointer to the
beginning of the allocated block.
• malloc(): The malloc() function is used to allocate a specified amount of
memory. It stands for "memory allocation." The syntax for malloc() is as
follows:
• #include <stdlib.h>
• void* malloc(size_t size);
• The malloc() function takes a single argument size of type size_t,
representing the number of bytes to allocate. It returns a void* pointer
to the allocated memory block. Here's an example:
• #include <stdio.h>
• #include <stdlib.h>
• int main() {
• int* ptr;
• // Allocate memory for an array of 5 integers
• ptr = (int*)malloc(5 * sizeof(int));
• if (ptr == NULL) {
• printf("Memory allocation failed\n");
• return 1;
• }
• In this example, malloc(5 * sizeof(int)) is used to allocate memory for
an array of 5 integers. The total memory required is calculated by
multiplying the number of elements (5) by the size of each element
(sizeof(int)).
• calloc(): The calloc() function is used to allocate memory for multiple
elements, typically in an array-like structure. It stands for "contiguous
allocation." The syntax for calloc() is as follows:
SYNTAX

• #include <stdlib.h>
• void* calloc(size_t num, size_t size);

• The calloc() function takes two arguments: num and size. num specifies
the number of elements to allocate, and size specifies the size of each
element. It returns a void* pointer to the allocated memory block with
all bytes initialized to zero.
• #include <stdio.h>
• #include <stdlib.h>

• int main() {
• int* ptr;

• // Allocate memory for an array of 5 integers


• ptr = (int*)calloc(5, sizeof(int));
• if (ptr == NULL) {
• printf("Memory allocation failed\n");
• return 1;
• }
REALLOC:

• realloc: It is used to resize an already allocated


memory block. It takes two arguments: the pointer
to the previously allocated block and the new size. If
the new size is larger, it may extend the block in-
place or allocate a new block and copy the contents.
If the new size is smaller, it may truncate the block.
• // Allocate memory for 3 integers
• ptr = (int*)malloc(3 * sizeof(int));
• // Resize the memory block to store 5 integers
• ptr = (int*)realloc(ptr, 5 * sizeof(int));
FREE:

• free: It is used to deallocate the dynamically allocated memory. When


you no longer need a dynamically allocated block, you should use free
to release the memory and make it available for reuse.
• #include <stdio.h>

• #include <stdlib.h>

• int main() {

• int* ptr;

• // Allocate memory for an integer

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

• if (ptr == NULL) {

• printf("Memory allocation failed\n");

• return 1;

• }

• // Assign a value to the allocated memory

• *ptr = 42;

• // Print the value

• printf("Value: %d\n", *ptr);

• // Deallocate the memory

• free(ptr);

• return 0;

• }

You might also like