Topic :Dynamic Memory allocation using Calloc() function in C++ Programming Language:

Calloc():

In Our Previous Tutorials, we discuss about Dynamic memory allocation using Malloc() function. In this tutorial we will allocate Dynamic Memory using Malloc() Function. Old C Library <stdlib.h> Provide Malloc() function to allocate Runtime Memory.

Same like New Operator, and Malloc() function, Calloc() Function allocate memory Dynamically and returns a pointer to the first byte of memory of specified size allocated from the heap. and return NULL if there is no requested space available.




The Only Difference In Malloc() and Calloc() is that, Malloc() function allocate single block of memory does not set the Memory to zero. Where as calloc() allocate multiple blocks of memory and sets Allocated memory to zero.



The Basic syntax for Calloc() Function is:



void *calloc(num, size);


In above syntax, num is the Numbers of Elements to be allocated, and size is the size of elements. The above function returns a void pointer to the allocated Memory. if there is insufficient memory available, then it will return NULL.

If we want Pointer to return other type then void, then we can use type cast Operator on the return value. The syntax is here:




cast_type *ptr; //Pointer to Data type;
ptr=(cast_type *)calloc(num, size);

//Example:

int *ptr;
ptr=(int*)calloc(num, sizeof(int));

In Above Syntax, We declared an Pointer to int, calloc() function need the size in bytes as argument, so we can use sizeof() operator to allocate the exact size.



Let's take an example here to understand, how this function work to allocate memory dynamically.



/*Example: Calloc() function: InfoBrother*/

#include<iostream>
#include<stdlib.h> //required for calloc() function.

using namespace std;

main()
{
    int *ptr, *ptr2; //Ptr will used to allocate memory and record the data.
     //ptr2 will used to display the data.
    
    int num, sum=0; //num=number of elements, sum=total score to display.
    
    cout<<"Enter total Numbers of elements to store: "<<endl;
    cout<<"Total Elements: ";
    cin>>num;
    
    ptr=(int*)calloc(num , sizeof(int));
    ptr=ptr2;
    cout<<" \n Enter Your "<<num<<" Elements to store: "<<endl;
    for(int i=0; i<=num; i++)
    {
        cout<<" Element # "<<i<<") ";
        cin>>*ptr;
        sum+=*ptr; //add all entered numbers.
        ptr++;
    }
    
    cout<<"\n\n Your Elements are: "<<endl;
    for(int i=0; i<=num; i++)
    {
        cout<<" Element # "<<i<<" ) ";
        cout<<*ptr2<<endl;
        *ptr2++;
    }
     cout<<" And the sum is: "<<sum;
    
    return 0;
    
}


The Above example is just a simple and basic example. we just allocate some memory dynamically using Calloc() function. This function allocates enough memory from the heap and store some element and return their sum. as We know that if Calloc() Function is unable to allocate the requested memory, it will return a NULL pointer, and a NULL pointer points Definitely No where, so if we try to use that NULL pointer that's point to nothing, then our Program probably won't last long, so its always better if our Programs contain an error checker, so we can know if Calloc() is unable to allocate requested memory, so after adding error checker in our program, the Program will look like.



/*Example: Calloc() function: InfoBrother*/

#include<iostream>
#include<stdlib.h> //required for calloc() function.

using namespace std;

main()
{
    int *ptr, *ptr2; //Ptr will used to allocate memory and record the data.
     //ptr2 will used to display the data.
    
    int num, sum=0; //num=number of elements, sum=total score to display.
    
    cout<<"Enter total Numbers of elements to store: "<<endl;
    cout<<"Total Elements: ";
    cin>>num;
    
    ptr=(int*)calloc(num , sizeof(int));
    ptr=ptr2;
    
    if(ptr==NULL) //Error Checker:
    {
        cout<<" Error: calloc() is not able to allocate Memory: "<<endl;
    }
    
    else
    {
        cout<<" \n Enter Your "<<num<<" Elements to store: "<<endl;
        for(int i=0; i<=num; i++)
        {
            cout<<" Element # "<<i<<") ";
            cin>>*ptr;
            sum+=*ptr; //add all entered numbers.
            ptr++;
        }
    
        cout<<"\n\n Your Elements are: "<<endl;
        for(int i=0; i<=num; i++)
        {
            cout<<" Element # "<<i<<" ) ";
            cout<<*ptr2<<endl;
            *ptr2++;
        }
         cout<<" And the sum is: "<<sum;
    }
    
    return 0;
    
}


Remember, We allocate Memory dynamically using calloc() function. This memory came from a heap, as it's from heap so we are responsible to release this memory once it's free and we don't need it more...



Now at the end we need to release the allocated memory, same like Malloc() function, we will use free() function to release the memory that was allocated using calloc() function. Now let's back to same example and try to free the memory that was allocated Dynamically.



/*Example: Calloc() function: InfoBrother*/

#include<iostream>
#include<stdlib.h> //required for calloc() function.

using namespace std;

main()
{
    int *ptr, *ptr2; //Ptr will used to allocate memory and record the data.
     //ptr2 will used to display the data.
    
    int num, sum=0; //num=number of elements, sum=total score to display.
    
    cout<<"Enter total Numbers of elements to store: "<<endl;
    cout<<"Total Elements: ";
    cin>>num;
    
    ptr=(int*)calloc(num , sizeof(int));
    ptr=ptr2;
    
    if(ptr==NULL) //Error Checker:
    {
        cout<<" Error: calloc() is not able to allocate Memory: "<<endl;
    }
    
    else
    {
        cout<<" \n Enter Your "<<num<<" Elements to store: "<<endl;
        for(int i=0; i<=num; i++)
        {
            cout<<" Element # "<<i<<") ";
            cin>>*ptr;
            sum+=*ptr; //add all entered numbers.
            ptr++;
        }
    
        cout<<"\n\n Your Elements are: "<<endl;
        for(int i=0; i<=num; i++)
        {
            cout<<" Element # "<<i<<" ) ";
            cout<<*ptr2<<endl;
            *ptr2++;
        }
         cout<<" And the sum is: "<<sum;
    }
    
    free(ptr); //free the Memory;
    
    return 0;
    
}








I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement