#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
int n = 5,m;
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]);
}
m=n+5;
ptr = (int *)calloc(m, sizeof(int));
printf("\n");
printf("After realloc values:\n");
for (int i = 0; i <m ; i++) {
printf("%d ", ptr[i]);
}
return 0;
}
🎨 Step 1: Header Files
#include <stdio.h>
#include <stdlib.h>
📖 Explanation
| Header | Purpose |
|---|---|
📘 stdio.h | Used for printf() and scanf() |
📗 stdlib.h | Used for dynamic memory functions like calloc(), malloc(), realloc(), and 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,m;
Explanation
| Variable | Meaning |
|---|---|
ptr | Pointer to dynamically allocated integer array |
n | Number of elements |
m | New size after increasing memory |
Initially
ptr
↓
NULL
n = 5
🎨 Step 4: Read Array Size
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)
Suppose
n = 5
sizeof(int)=4 bytes
Memory allocated
5 × 4 = 20 bytes
Memory Layout
ptr
↓
+-----+-----+-----+-----+-----+
| 0 | 0 | 0 | 0 | 0 |
+-----+-----+-----+-----+-----+
✔ calloc() initializes every byte to 0.
🎨 Step 6: Check Memory Allocation
if(ptr==NULL)
{
printf("Memory allocation failed!");
return 1;
}
If memory allocation fails
ptr = NULL
Program terminates safely.
🎨 Step 7: Store Values
for(int i=0;i<n;i++)
{
ptr[i]=i*10+10;
}
Calculation
| i | Formula | 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 becomes
ptr
↓
+----+----+----+----+----+
|10 |20 |30 |40 |50 |
+----+----+----+----+----+
🎨 Step 8: Print Stored Values
for(int i=0;i<n;i++)
{
printf("%d ",ptr[i]);
}
Output
10 20 30 40 50
🎨 Step 9: Print Extra Index
for(int i=0;i<n+5;i++)
{
printf("%d ",ptr[i]);
}
If
n=5
Loop runs
0
1
2
3
4
5
6
7
8
9
But allocated memory only contains
0
1
2
3
4
Memory
Allocated
+----+----+----+----+----+
|10 |20 |30 |40 |50 |
+----+----+----+----+----+
Accessing
5
6
7
8
9
❌ Not Allocated
⚠️ Undefined Behaviour
Possible output
10 20 30 40 50 0 12345 -8 456 0
or
Program Crash
or
Garbage Values
This is unsafe and should never be done.
🎨 Step 10: Increase Size
m=n+5;
Example
n=5
m=10
🎨 Step 11: Allocate New Memory
ptr=(int *)calloc(m,sizeof(int));
This allocates new memory for 10 integers.
⚠ Problem: The previously allocated memory is lost, causing a memory leak because it was not freed before overwriting ptr.
The new memory contains all zeros.
+---+---+---+---+---+---+---+---+---+---+
|0 |0 |0 |0 |0 |0 |0 |0 |0 |0 |
+---+---+---+---+---+---+---+---+---+---+
🎨 Step 12: Print New Memory
for(int i=0;i<m;i++)
{
printf("%d ",ptr[i]);
}
Output
0 0 0 0 0 0 0 0 0 0
because calloc() initializes memory to zero.
🖥️ 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 4198544 -2 0 1048576
After realloc values:
0 0 0 0 0 0 0 0 0 0
Note: The values after
50are unpredictable because the program reads beyond the allocated memory. They may differ each time you run the program.
📊 Memory Diagram
After First calloc()
ptr
↓
+------+------+------+------+------+
| 10 | 20 | 30 | 40 | 50 |
+------+------+------+------+------+
Illegal Access
Index
0 1 2 3 4 |5 6 7 8 9
↑
Undefined Behaviour
After Second calloc()
ptr
↓
+---+---+---+---+---+---+---+---+---+---+
|0 |0 |0 |0 |0 |0 |0 |0 |0 |0 |
+---+---+---+---+---+---+---+---+---+---+