#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
int n = 5;
printf("Enter number of values:\n");
scanf("%d",&n);
ptr = (int *)calloc(n, sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
printf("\n");
for (int i = 0; i < n; i++) {
ptr[i] = i *10+10;
}
printf("After assigning values:\n");
for (int i = 0; i <n ; i++) {
printf("%d ", ptr[i]);
}
printf("\n");
printf(" values with extra index:\n");
for (int i = 0; i <n +5; i++) {
printf("%d ", ptr[i]);
}
free(ptr);
printf("\n");
printf("After free values:\n");
for (int i = 0; i <n ; i++) {
printf("%d ", ptr[i]);
}
return 0;
}
🟦 Step 1: Include Header Files
#include <stdio.h>
#include <stdlib.h>
Explanation
| Header File | Purpose |
|---|---|
stdio.h | Provides input/output functions (printf(), scanf()) |
stdlib.h | Provides memory management functions (calloc(), malloc(), realloc(), free()) |
🟦 Step 2: Main Function
int main()
Every C program starts execution from the main() function.
🟦 Step 3: Declare Variables
int *ptr;
int n = 5;
Explanation
-
ptris an integer pointer. - It will point to dynamically allocated memory.
-
nstores the number of integers to allocate.
Initially:
ptr
↓
NULL (not allocated yet)
n = 5
🟦 Step 4: Read Number of Elements
printf("Enter number of values:\n");
scanf("%d",&n);
Example Input
5
Now,
n = 5
🟦 Step 5: Allocate Memory using calloc()
ptr = (int *)calloc(n, sizeof(int));
Syntax
calloc(Number_of_elements, Size_of_each_element)
If
n = 5
sizeof(int) = 4 bytes
Then memory allocated is
5 × 4 = 20 bytes
Memory Diagram
ptr
↓
+----+----+----+----+----+
| 0 | 0 | 0 | 0 | 0 |
+----+----+----+----+----+
Important:
calloc()initializes all allocated memory to 0.
🟦 Step 6: Check Allocation
if(ptr == NULL)
{
printf("Memory allocation failed!\n");
return 1;
}
Why?
Sometimes the operating system cannot provide memory.
If allocation fails,
ptr = NULL
The program exits safely.
🟦 Step 7: Store Values
for(int i=0;i<n;i++)
{
ptr[i]=i*10+10;
}
Calculations
| i | Formula | Stored Value |
|---|---|---|
| 0 | 0×10+10 | 10 |
| 1 | 1×10+10 | 20 |
| 2 | 2×10+10 | 30 |
| 3 | 3×10+10 | 40 |
| 4 | 4×10+10 | 50 |
Memory
ptr
↓
+----+----+----+----+----+
|10 |20 |30 |40 |50 |
+----+----+----+----+----+
🟦 Step 8: Print Values
for(int i=0;i<n;i++)
{
printf("%d ",ptr[i]);
}
Output
10 20 30 40 50
🟦 Step 9: Print Beyond Allocated Memory
for(int i=0;i<n+5;i++)
{
printf("%d ",ptr[i]);
}
Here,
n = 5
Loop runs from
0 to 9
But allocated memory only contains
Index
0
1
2
3
4
The program also accesses
5
6
7
8
9
which are outside the allocated memory.
Memory Representation
Allocated Memory
+----+----+----+----+----+
|10 |20 |30 |40 |50 |
+----+----+----+----+----+
Invalid Access
Index
5
6
7
8
9
⚠ Undefined Behaviour
Possible outputs:
10 20 30 40 50 0 1024 -5 12345
or
Segmentation Fault
or
Garbage Values
The result is unpredictable.
🟦 Step 10: Free Allocated Memory
free(ptr);
What happens?
The memory allocated by calloc() is returned to the operating system.
Before free():
ptr
↓
+----+----+----+----+----+
|10 |20 |30 |40 |50 |
+----+----+----+----+----+
After free():
Memory Released
ptr
↓
Dangling Pointer
The pointer still contains the old address, but that memory is no longer valid.
🟦 Step 11: Access Memory After free()
for(int i=0;i<n;i++)
{
printf("%d ",ptr[i]);
}
This is incorrect.
The memory has already been released.
Possible output:
10 20 30 40 50
or
0 0 0 0 0
or
Garbage Values
or
Segmentation Fault
This is another example of Undefined Behaviour.
📊 Complete Memory Flow
1 Before Allocation
ptr
↓
NULL
2. After calloc()
ptr
↓
+----+----+----+----+----+
| 0 | 0 | 0 | 0 | 0 |
+----+----+----+----+----+
3.After Storing Values
ptr
↓
+----+----+----+----+----+
|10 |20 |30 |40 |50 |
+----+----+----+----+----+
4.After free()
Memory Returned to OS
ptr
↓
Dangling Pointer
🖥 Sample Execution
Input
Enter number of values:
5
Output
After assigning values:
10 20 30 40 50
Values with extra index:
10 20 30 40 50 0 -1452672 8 0 245
After free values:
-1452672 8 0 245 1
No comments:
Post a Comment