#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");
free(ptr);
printf("after free ptr new value = %d ", *ptr);
return 0;
}
🟦 Step 1: Include Header Files
#include <stdio.h>
#include <stdlib.h>
📖 Explanation
| Header File | Purpose |
|---|---|
📘 stdio.h | Used for printf() and scanf() |
📗 stdlib.h | Used for malloc(), calloc(), realloc(), and free() |
🟦 Step 2: Declare Pointer
int *ptr;
Explanation
-
ptris an integer pointer. - It will store the address of dynamically allocated memory.
Initially
ptr
↓
Uninitialized
🟦 Step 3: Allocate Memory
ptr = (int*) malloc(sizeof(int));
Explanation
malloc() allocates memory for one integer.
If
sizeof(int) = 4 bytes
then
malloc(4)
allocates 4 bytes of memory.
Memory Diagram
ptr
↓
+---------+
| Garbage |
+---------+
Important: Unlike
calloc(),malloc()does not initialize memory. The allocated memory contains a garbage value until you assign one.
🟦 Step 4: Check Memory Allocation
if(ptr == NULL)
{
printf("Memory allocation failed!");
return 1;
}
Explanation
If the operating system cannot allocate memory,
ptr
↓
NULL
The program terminates safely.
🟦 Step 5: Read User Input
scanf("%d", ptr);
Suppose the user enters
50
Memory becomes
ptr
↓
+------+
| 50 |
+------+
🟦 Step 6: Print the Value
printf("Value = %d", *ptr);
*ptr means:
The value stored at the memory location pointed to by
ptr.
Output
Value = 50
🟦 Step 7: Free the Memory
free(ptr);
Explanation
The allocated memory is returned to the operating system.
Before free()
ptr
↓
+------+
| 50 |
+------+
After free()
Memory Released
ptr
↓
Dangling Pointer
The pointer still contains the old address, but the memory is no longer valid.
🟥 Step 8: Accessing Memory After free()
printf("After free ptr new value = %d", *ptr);
❌ This is Incorrect
After free(), the pointer refers to memory that has already been released.
Accessing it causes Undefined Behaviour.
Possible outputs:
After free ptr new value = 50
or
After free ptr new value = 0
or
After free ptr new value = -125678
or
Segmentation Fault
There is no guaranteed result.
🌈 Memory Flow
🔹 Before malloc()
ptr
↓
Uninitialized
⬇
🔹 After malloc()
ptr
↓
+---------+
| Garbage |
+---------+
⬇
🔹 After User Input
ptr
↓
+------+
| 50 |
+------+
⬇
🔹 After free()
Memory Released
ptr
↓
Dangling Pointer
⬇
🔹 Accessing *ptr
❌ Undefined Behaviour
🖥 Sample Execution
Input
Enter a value:
50
Possible Output
Memory successfully allocated using malloc.
Enter a value:
50
Value = 50
After free ptr new value = 50
On another system or another run, the last line may show a different value or the program may crash.
⚠ Problems in the Program
Problem
free(ptr);
printf("%d", *ptr);
The memory has already been released.
The pointer becomes a dangling pointer.
Reading from it is undefined behaviour according to the C standard.
✅ Correct Version
free(ptr);
ptr = NULL;
if(ptr != NULL)
{
printf("%d", *ptr);
}
else
{
printf("Pointer is NULL. Memory has been freed.");
}
Output
Memory successfully allocated using malloc.
Enter a value:
50
Value = 50
Pointer is NULL. Memory has been freed.
No comments:
Post a Comment