0% found this document useful (0 votes)
1 views

T2402100330034 DynamicMemoryAllocation

Uploaded by

attitudeking565
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)
1 views

T2402100330034 DynamicMemoryAllocation

Uploaded by

attitudeking565
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/ 12

Dynamic Memory Allocation in C

Presented by : Vraj Panchal


Enrollment : T2402100330034

Prof. Yogeshwari Makawana


What is Dynamic Memory Allocation?
Dynamic memory allocation is the process of managing system
memory during program execution, or runtime. It allows you to allocate
and release memory blocks as needed, rather than having to estimate
how much memory you'll need at compile time. This is useful in
situations where you don't know how much data your program will
receive or if the size of an array needs to change.

For example: Dynamic memory allocation is when a program manages


memory while it's running, instead of deciding how much it needs
before it starts.
Dynamic Memory Allocation Advantages
Dynamic memory allocation has some great benefits in C programming:

Flexibility Efficiency Data Structures


You can allocate By only using It allows you to
memory as needed, memory when create dynamic data
so your program can required, you reduce structures like linked
adapt to changing waste and use lists that can grow or
requirements. resources better. shrink.
Disadvantages of Dynamic Memory Allocation
There are some disadvantage of Dynamic memory allocation :

Memory Leaks Security Risks Performance Overhead


If memory is allocated Improper memory Allocating and freeing
but not freed properly, it management can create memory dynamically
can lead to memory vulnerabilities, such as can introduce
leaks, gradually buffer overflows, which performance overhead,
consuming available can be exploited by especially for frequent
memory. attackers. allocations and
deallocations.
Dynamic Memory
Allocation Functions
C provides several standard library functions for
managing dynamic memory.

malloc() Allocates a single block


of memory of a
specified size.
calloc() Allocates multiple block
of memory.

realloc() Reallocate memory that


malloc() or calloc()
occupy.
free() Releases a block of
dynamically allocated
memory.
Code Example 1: Allocating Memory Dynamically
How to allocate memory dynamically using the malloc() function.
#include <stdio.h>
#include <stdlib.h>

int main() {
int* ptr = (int*) malloc(sizeof(int)); //Allocate the memory for an integer
if (ptr == NULL) {
printf("Memory allocation failed.\n"); //To check if memory allocation was failed or not
return -1;
}
*ptr = 10; //Assign a value to the allocated memory
printf("Value at ptr: %d\n", *ptr);

free(ptr); //Free the allocated memory


return 0;
}
Code Example 2: Allocating Memory Dynamically
How to do contiguous allocation using the calloc() function.
#include <stdio.h>
#include <stdlib.h>
int main() {
int* ptr = (int*) calloc(5, sizeof(int)); //Allocate the memory for an integer
if (ptr == NULL) {
printf("Memory allocation failed.\n"); //To check if memory allocation was failed or not
return -1;
}
for (int i = 0; i < 5; i++) {
ptr[i] = i * 2; //Assign values to the allocated memory
}
for (int i = 0; i < 5; i++) {
printf(“Allocated memory value index %d: %d\n”, i, ptr[i]);
}
free(ptr); //Free the allocated memory
return 0;
}
Code Example 3: Reallocating Memory Dynamically
How to resize an existing memory block using the realloc() function.
#include <stdio.h>
#include <stdlib.h>

int main() {
int* ptr = malloc(2 * sizeof(int));
ptr[0] = 10;
ptr[1] = 20;

ptr = realloc(ptr, 5 * sizeof(int));


ptr[2] = 30;
ptr[3] = 40;
ptr[4] = 50;

for (int i = 0; i < 5; i++) {


printf("%d ", ptr[i]);
}

free(ptr);
return 0;
}
Code Example 4: Freeing Dynamically Allocated Memo
How to free dynamically allocated memory using free() function.

#include <stdio.h>
#include <stdlib.h>

int main() {
int* ptr = malloc(sizeof(int));
*ptr = 10;
printf("Allocated memory: %d\n", *ptr);
free(ptr);
return 0;
}
Code Example 5: Dynamically Allocated Memory
How to allocate memory dynamically using malloc(), calloc(), realloc() and free(
#include <stdio.h>
#include <stdlib.h>

int main() {
int* ptr1 = (int*)malloc(sizeof(int));
*ptr1 = 10;
printf("Memory allocated using malloc: %d\n", *ptr1);
free(ptr1);

int* ptr2 = (int*)calloc(1, sizeof(int));


*ptr2 = 20;
printf("Memory allocated and initialized using calloc: %d\n", *ptr2);
free(ptr2);

int* ptr3 = (int*)malloc(sizeof(int));


*ptr3 = 30;
printf("Memory allocated using malloc: %d\n", *ptr3);

ptr3 = (int*)realloc(ptr3, 2 * sizeof(int));


ptr3[0] = 30; // Retain the original value
ptr3[1] = 40;
printf("Memory reallocated using realloc: %d %d\n", ptr3[0], ptr3[1]);
free(ptr3);

return 0;
}
Conclusion
Dynamic memory allocation in C offers flexibility and efficiency but
requires careful management. Understanding the advantages,
disadvantages, and proper usage of dynamic memory allocation
functions is essential for writing robust and secure C programs.
Thank you

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