0% found this document useful (0 votes)
18 views12 pages

Dynamic Memory Allocation in C

dynamic memory allocation in c

Uploaded by

mathu
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)
18 views12 pages

Dynamic Memory Allocation in C

dynamic memory allocation in c

Uploaded by

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

Dynamic Memory Allocation in C

Overview and Examples


Introduction to Dynamic Memory
Allocation
• Dynamic memory allocation in C allows
programs to allocate memory at runtime,
providing flexibility in managing memory for
cases where memory requirements are
unknown before program execution.
• Functions: malloc(), calloc(), realloc(), and
free().
malloc(): Memory Allocation
• Syntax: void* malloc(size_t size);

• Purpose: Allocates a specified amount of


memory (in bytes) and returns a pointer to it.
• - Does not initialize memory
• - Returns NULL if allocation fails

• Example:
• int *arr = (int*) malloc(5 * sizeof(int));
calloc(): Contiguous Allocation
• Syntax: void* calloc(size_t num, size_t size);

• Purpose: Allocates memory for an array of


elements, initializing all to zero.
• - Returns NULL if allocation fails

• Example:
• int *arr = (int*) calloc(5, sizeof(int));
realloc(): Re-allocate Memory
• Syntax: void* realloc(void* ptr, size_t new_size);

• Purpose: Changes the size of the previously


allocated block of memory.
• - Can increase or decrease allocated memory
• - Returns NULL if reallocation fails

• Example:
• arr = (int*) realloc(arr, 10 * sizeof(int));
free(): Deallocate Memory
• Syntax: void free(void* ptr);

• Purpose: Frees memory allocated at the given


pointer to avoid memory leaks.
• - Passing NULL is safe and has no effect

• Example:
• free(arr); arr = NULL;
Example Program: Dynamic Array
Allocation
• This example uses malloc and free to dynamically create and manage an
integer array.
• Code:
• #include <stdio.h>
• #include <stdlib.h>

• int main() {
• int n, i;
• int *arr = (int*) malloc(n * sizeof(int));
• if (arr == NULL) { return 1; }
• for (i = 0; i < n; i++) { scanf("%d", &arr[i]); }
• free(arr);
• return 0;
• }
Important Concepts
• Memory Leak: Allocated memory that is not
freed, causing memory wastage.
• Dangling Pointer: Pointer pointing to freed
memory, leading to undefined behavior.
• NULL Pointer: Passing a NULL pointer to free()
is safe and avoids accidental access.
Benefits of Dynamic Memory
Allocation
• 1. Flexibility: Memory allocation based on
runtime requirements.
• 2. Efficient Memory Use: Minimizes wasted
memory.
• 3. Suitable for Variable Sizes: Allows dynamic
adjustment of arrays and other structures.
int main()
{
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
int *arr = (int*) malloc(n * sizeof(int)); // Allocates memory for n integers
if (arr == NULL)
{
printf("Memory allocation failed\n");
return 1; // Exit if memory allocation fails
}
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printf("You entered: ");
for (i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
free(arr); // Free the allocated memory return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, i;
printf("Enter the number of elements: "); scanf("%d", &n);
int *arr = (int*) calloc(n, sizeof(int)); // Allocates and initializes n
integers to zero
if (arr == NULL)
{
printf("Memory allocation failed\n");
return 1;
}
printf("Array elements initialized to zero:\n");
for (i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
free(arr); // Free allocated memory
return 0;
}
#include <stdio.h> printf("Resized array: ");
#include <stdlib.h> for (i = 0; i < new_n; i++)
int main() { printf("%d ", arr[i]); } printf("\n");
{ int n, new_n, i; free(arr); // Free allocated memory return 0; }
printf("Enter initial number of elements: ");
scanf("%d", &n);
int *arr = (int*) malloc(n * sizeof(int));
if (arr == NULL)
{
printf("Memory allocation failed\n");
return 1; }
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++)
{ scanf("%d", &arr[i]); }
printf("Enter new size for the array: ");
scanf("%d", &new_n);
arr = (int*) realloc(arr, new_n * sizeof(int)); //
Resize the memory block
if (arr == NULL)
{ printf("Memory reallocation failed\n");
return 1; }
printf("Enter additional elements:\n");
for (i = n; i < new_n; i++)
{ scanf("%d", &arr[i]); }

You might also like