DMCA.com Protection Status C Programming – Day 11 | Dynamic Memory Allocation: malloc, calloc, realloc, free, C Explained - ZAREEN TECH WAVE -Largest Tutorials Website For Freelancer ZAREEN TECH WAVE -Largest Tutorials Website For Freelancer

Header Ads

C Programming – Day 11 | Dynamic Memory Allocation: malloc, calloc, realloc, free, C Explained

DYNAMIC MEMORY ALLOCATION IN C

DAY - 11


Learn C Programming Day 11 with a detailed guide on dynamic memory allocation in C. Understand how to use malloc, calloc, realloc, and free to manage memory for arrays, structures, and other data types.

✅ Let’s move to Day-11 — Dynamic Memory Allocation in C 🚀


📍 Day-11 — Dynamic Memory Allocation in C

Today we will learn:

✅ What is Dynamic Memory Allocation
✅ Difference between static & dynamic memory
malloc(), calloc(), realloc(), free()
✅ Task + Quiz


✅ 1️⃣ What is Dynamic Memory Allocation?

📌 Memory is allocated at runtime instead of compile time.
📌 Useful when you don’t know array size in advance.

  • Static memory → compile-time, fixed size

  • Dynamic memory → runtime, flexible


✅ 2️⃣ malloc() — Memory Allocation

#include <stdio.h> #include <stdlib.h> int main() { int *ptr; int n; printf("Enter number of elements: "); scanf("%d", &n); ptr = (int*) malloc(n * sizeof(int)); for(int i=0; i<n; i++){ scanf("%d", &ptr[i]); } for(int i=0; i<n; i++){ printf("%d ", ptr[i]); } free(ptr); // release memory }

✅ 3️⃣ calloc() — Contiguous Allocation

int *ptr; ptr = (int*) calloc(n, sizeof(int)); // initializes to 0

✅ Difference: calloc initializes memory to zero, malloc doesn’t


✅ 4️⃣ realloc() — Resize Memory

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

✅ Useful if you want to increase/decrease array size at runtime


✅ 5️⃣ free() — Free Memory

free(ptr); // Always free dynamic memory to avoid memory leak

✅ Task-11

Write a program:

✔ Input number of elements from user
✔ Allocate memory using malloc()
✔ Input elements and print them
✔ Reallocate memory to add 2 more elements using realloc()
✔ Print updated array

Example Output:

Original Array: 10 20 30 After Realloc: 10 20 30 40 50

📝 Quick Quiz — Day-11

Reply like: 1B, 2A, 3C

1️⃣ Function to allocate memory dynamically:
A) free()
B) malloc()
C) scanf()

2️⃣ calloc() initializes memory to:
A) Garbage value
B) 0
C) 1

3️⃣ Function to free allocated memory:
A) release()
B) delete()
C) free()


📌 Reminder:

Send your Task-11 Code and Day-11 Quiz Answers when ready!

Then we will proceed to Day-12: Final Projects & Revision 🎯





C Programming Day 11 covers dynamic memory allocation in C, including malloc, calloc, realloc, and free. Learn how to allocate and manage memory efficiently for arrays, structures, and other data types in C programming.

No comments

Thank You For Visit My Website.
I Will Contact As Soon As Possible.

Theme images by 5ugarless. Powered by Blogger.