Total Pageviews

Tuesday, December 2, 2025

Realloc

 #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

HeaderPurpose
📘 stdio.hUsed for printf() and scanf()
📗 stdlib.hUsed 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

VariableMeaning
ptrPointer to dynamically allocated integer array
nNumber of elements
mNew 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

iFormulaValue
00×10+1010
11×10+1020
22×10+1030
33×10+1040
44×10+1050

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 50 are 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 |
+---+---+---+---+---+---+---+---+---+---+




Calloc and free

 #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 FilePurpose
stdio.hProvides input/output functions (printf(), scanf())
stdlib.hProvides 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

  • ptr is an integer pointer.
  • It will point to dynamically allocated memory.
  • n stores 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

iFormulaStored Value
00×10+1010
11×10+1020
22×10+1030
33×10+1040
44×10+1050

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