#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
ptr = (int*) malloc(sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
printf("Memory successfully allocated using malloc.\n");
printf("enter a value.\n");
scanf("%d",ptr);
printf("value = %d ", *ptr);
printf("\n");
return 0;
}
🟦 Step 1 : Include Header Files
#include <stdio.h>
#include <stdlib.h>
📖 Explanation
| 📚 Header File | 🎯 Purpose |
|---|---|
🟦 stdio.h | Used for input/output functions like printf() and scanf(). |
🟩 stdlib.h | Used for memory management functions such as malloc(), calloc(), realloc(), and free(). |
🟦 Step 2 : Start the Program
int main()
📖 Explanation
Every C program begins execution from the main() function.
Program Starts
│
▼
int main()
🟦 Step 3 : Declare a Pointer
int *ptr;
📖 Explanation
-
ptris an integer pointer. - It will store the address of dynamically allocated memory.
Initially
ptr
↓
Garbage Address (Uninitialized)
🟦 Step 4 : Allocate Memory Using malloc()
ptr = (int*) malloc(sizeof(int));
📌 Syntax
malloc(Number_of_Bytes)
Example
sizeof(int) = 4 bytes
Memory allocated
4 Bytes
🖼 Memory Representation
Before Allocation
ptr
↓
?
After Allocation
ptr
↓
+---------+
| Garbage |
+---------+
⭐ Important
Unlike calloc(), malloc() does NOT initialize memory.
The allocated memory contains garbage (unpredictable) values until you assign a value.
🟦 Step 5 : Check Memory Allocation
if (ptr == NULL)
{
printf("Memory allocation failed!\n");
return 1;
}
📖 Explanation
If memory allocation fails,
ptr
↓
NULL
The program prints:
Memory allocation failed!
and terminates safely.
🟦 Step 6 : Display Success Message
printf("Memory successfully allocated using malloc.\n");
Output
Memory successfully allocated using malloc.
This confirms that memory allocation was successful.
🟦 Step 7 : Read a Value
printf("Enter a value:\n");
scanf("%d", ptr);
Suppose the user enters
25
The value 25 is stored in the allocated memory.
🖼 Memory Diagram
Before Input
ptr
↓
+---------+
| Garbage |
+---------+
After Input
ptr
↓
+------+
| 25 |
+------+
🟦 Step 8 : Print the Stored Value
printf("Value = %d", *ptr);
Explanation
*ptr means the value stored at the memory location pointed to by ptr.
If
ptr = 1000 (Address)
Memory
1000 → 25
Then
*ptr
returns
25
Output
Value = 25
🌈 Complete Memory Flow
🔹 Before Allocation
ptr
↓
Uninitialized
⬇
🔹 After malloc()
ptr
↓
+---------+
| Garbage |
+---------+
⬇
🔹 After User Input
ptr
↓
+------+
| 25 |
+------+
⬇
🔹 After Printing
Value = 25
🖥 Sample Execution
Input
Enter a value:
50
Output
Memory successfully allocated using malloc.
Enter a value:
50
Value = 50
📊 malloc() Memory Representation
malloc(sizeof(int))
│
▼
+---------------+
ptr ------>| 25 |
+---------------+
🌟 Advantages of malloc()
✅ Allocates memory during program execution.
✅ Useful when memory size is determined at runtime.
✅ Efficient because it does not initialize memory.
✅ Widely used for dynamic arrays, linked lists, trees, and other dynamic data structures.
⚠ Best Practices
✔ Always check if malloc() returns NULL.
✔ Initialize the allocated memory before using it.
✔ Release allocated memory using free().
✔ After free(), assign the pointer to NULL.
🟦 Improved Version of the Program
Your program allocates memory correctly, but it should also release it after use.
free(ptr);
ptr = NULL;
Add these lines before return 0;.
No comments:
Post a Comment