Total Pageviews

Tuesday, November 25, 2025

Calloc

 //calloc


#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");


   

    free(ptr);

    ptr = NULL; 


    return 0;

}



🟦 Step 1 : Include Header Files

#include <stdio.h>
#include <stdlib.h>

📖 Explanation

📚 Header File🎯 Purpose
🟦 stdio.hUsed for input/output functions like printf() and scanf().
🟩 stdlib.hUsed for dynamic memory functions such as calloc(), malloc(), 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 Variables

int *ptr;
int n = 5;

📖 Explanation

VariableMeaning
🔹 ptrPointer that stores the address of dynamically allocated memory
🔹 nNumber of integer elements

Initially

ptr



NULL

n = 5

No memory has been allocated yet.


🟦 Step 4 : Take User Input

printf("Enter number of values:\n");
scanf("%d",&n);

Example Input

Enter number of values:
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)

Example

n = 5

sizeof(int)=4 bytes

Memory allocated

5 × 4 = 20 Bytes

🖼 Memory Before Allocation

ptr



NULL

🖼 Memory After calloc()

ptr



+------+------+------+------+------+
| 0 | 0 | 0 | 0 | 0 |
+------+------+------+------+------+

⭐ Important

calloc() initializes every element to 0 automatically.


🟦 Step 6 : Check Memory Allocation

if(ptr==NULL)
{
printf("Memory allocation failed!\n");
return 1;
}

📖 Explanation

If the operating system cannot allocate memory,

ptr



NULL

The program prints

Memory allocation failed!

and terminates safely.


🟦 Step 7 : Store Values

for(int i=0;i<n;i++)
{
ptr[i]=i*10+10;
}

📊 Value Calculation

🔢 iFormulaStored Value
00×10+10✅ 10
11×10+10✅ 20
22×10+10✅ 30
33×10+10✅ 40
44×10+10✅ 50

🖼 Memory After Storing Values

ptr



+------+------+------+------+------+
| 10 | 20 | 30 | 40 | 50 |
+------+------+------+------+------+

🟦 Step 8 : Print the Values

printf("After assigning values:\n");

for(int i=0;i<n;i++)
{
printf("%d ",ptr[i]);
}

Output

After assigning values:

10 20 30 40 50

🟦 Step 9 : Release Memory

free(ptr);

📖 Explanation

The free() function releases the allocated memory back to the operating system.

Before free()

ptr



+------+------+------+------+------+
| 10 | 20 | 30 | 40 | 50 |
+------+------+------+------+------+

After free()

Memory Released

ptr



(Dangling Pointer)

The memory no longer belongs to the program.


🟦 Step 10 : Assign NULL

ptr=NULL;

📖 Explanation

After freeing memory, the pointer still contains the old address.

Such a pointer is called a Dangling Pointer.

Assigning

ptr=NULL;

makes the pointer safe.


Before

ptr



0x1000A520

After

ptr



NULL

🌈 Complete Memory Flow

🔹 Before Allocation

ptr



NULL

🔹 After calloc()

ptr



+----+----+----+----+----+
| 0 | 0 | 0 | 0 | 0 |
+----+----+----+----+----+

🔹 After Assigning Values

ptr



+----+----+----+----+----+
|10 |20 |30 |40 |50 |
+----+----+----+----+----+

🔹 After free()

Memory Released

ptr



NULL

🖥 Sample Run

Input

Enter number of values:
5

Output

After assigning values:

10 20 30 40 50

🌟 Advantages of calloc()

✅ Allocates memory dynamically.

✅ Initializes all memory to 0.

✅ Prevents uninitialized (garbage) values.

✅ Useful when array size is known only during program execution.


No comments:

Post a Comment