0% found this document useful (0 votes)
4 views4 pages

Dynamic Memory Allocation in C

The document provides examples of dynamic memory allocation in C using malloc(), calloc(), realloc(), and free(). It demonstrates how to allocate memory for an array, handle memory allocation failures, and modify arrays using dynamically allocated memory. Additionally, it highlights the importance of freeing allocated memory to prevent memory leaks.

Uploaded by

Harish Karthik
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)
4 views4 pages

Dynamic Memory Allocation in C

The document provides examples of dynamic memory allocation in C using malloc(), calloc(), realloc(), and free(). It demonstrates how to allocate memory for an array, handle memory allocation failures, and modify arrays using dynamically allocated memory. Additionally, it highlights the importance of freeing allocated memory to prevent memory leaks.

Uploaded by

Harish Karthik
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

Dynamic Memory Alloca/on

malloc()

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

int main() {
int *arr;
int n;

prin;("Enter number of elements: ");


scanf("%d", &n);

arr = (int *) malloc(n * sizeof(int));

if (arr == NULL) {
prin;("Memory allocaJon failed!\n");
return 1;
}

prin;("Enter elements:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

prin;("You entered: ");


for (int i = 0; i < n; i++) {
prin;("%d ", arr[i]);
}

free(arr);
return 0;
}

calloc()

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

int main() {
int *arr;
int n;
prin;("Enter number of elements: ");
scanf("%d", &n);

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

if (arr == NULL) {
prin;("Memory allocaJon failed!\n");
return 1;
}

prin;("IniJal values: ");


for (int i = 0; i < n; i++) {
prin;("%d ", arr[i]); // all zeros
}

free(arr);
return 0;
}

realloc()

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

int main() {
int *arr;
int n;

prin;("Enter number of elements: ");


scanf("%d", &n);

arr = (int *) malloc(n * sizeof(int));

prin;("Enter %d numbers: ", n);


for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);

prin;("Enter new size: ");


int new_n;
scanf("%d", &new_n);

arr = (int *) realloc(arr, new_n * sizeof(int));

prin;("Enter extra numbers: ");


for (int i = n; i < new_n; i++)
scanf("%d", &arr[i]);

prin;("Final array: ");


for (int i = 0; i < new_n; i++)
prin;("%d ", arr[i]);

free(arr);
return 0;
}

free()
#include <stdio.h>
#include <stdlib.h>

int main() {
int *ptr = (int *) malloc(5 * sizeof(int));

if (ptr == NULL) {
prin;("Memory not allocated.\n");
return 1;
}

free(ptr); // release memory


ptr = NULL; // avoid dangling pointer

return 0;
}

#include <stdio.h>

int* modifyArray(int arr[], int n) {


int result[100]; // not possible…Local array (exists only inside this funcJon)

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


result[i] = arr[i] * 2;
}

return result;
}

int main() {
int a[] = {1, 2, 3, 4, 5};
int n = 5;

int *modified = modifyArray(a, n);

prin;("%d\n", modified[0]);
return 0;
}

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

int* modifyArray(int *arr, int n) {


int *result = (int*)malloc(n * sizeof(int));

if (result == NULL) {
prin;("Memory allocaJon failed\n");
return NULL;
}

// Copy and modify


for (int i = 0; i < n; i++) {
result[i] = arr[i] * 2;
}

return result;
}

int main() {
int a[] = {1, 2, 3, 4, 5};
int n = 5;

int *modified = modifyArray(a, n);

prin;("Modified array: ");


for (int i = 0; i < n; i++) {
prin;("%d ", modified[i]);
}

free(modified);

return 0;
}

You might also like