Thread: how to use calloc

  1. #1
    Registered User
    Join Date
    Feb 2022
    Posts
    73

    how to use calloc

    I understood how to use malloc and realloc in dynamic memory allocation but I don't understand how to use calloc ?

    I belive my code allocate dynamic memory for five integer variables.

    Code:
     #include<stdio.h>
    #include<stdlib.h>
    
    
    int main (void)
    {
      // memory is allocated for two integer variables using malloc    
       int * array = (int*) malloc( 2 * sizeof(*array)); 
      
        if ( array != NULL)// Check if the memory has been successfully 
       
            { 
               printf("Memory successfully allocated using malloc.\n"); 
                
                // memory is allocated for three integer variables using realloc    
                array = (int*) realloc ( array, 3 * sizeof(*array)); 
                
                if ( array != NULL)// Check if the memory has been successfully 
                {
                    printf("Memory successfully allocated using realloc.\n"); 
                }
            }
       
       return 0;
    }
    I know calloc also allocate memory, Is there any advice how to know use of calloc ?

  2. #2
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103
    calloc zeros the memory it allocates.

    From my manpages:
    The calloc() function allocates memory for an array of nmemb elements
    of size bytes each and returns a pointer to the allocated memory. The
    memory is set to zero.

  3. #3
    Registered User
    Join Date
    Feb 2022
    Posts
    73
    Quote Originally Posted by G4143 View Post
    calloc zeros the memory it allocates.

    From my manpages:
    Thank you,

    program proves how calloc is different then malloc

    Code:
     #include<stdio.h>
    #include<stdlib.h>
    
    int main (void)
    {
       // memory is allocated for one integer variables using malloc    
       int *x = malloc(1 * sizeof(int));
       
       // memory is allocated for one integer variables using calloc    
       int *y = calloc(1, sizeof(int));
       
        if ( x != NULL)// Check if the memory has been successfully 
       
            { 
                printf("Memory successfully allocated using malloc.\n"); 
                 
                printf( "x = %d\n", *x);        
            }
            
        if ( y != NULL)// Check if the memory has been successfully 
            {
                printf("Memory successfully allocated using calloc.\n"); 
                
                printf( "y = %d\n", *y);
            }
       
       return 0;
    }
    Memory successfully allocated using malloc.
    x = 134472
    Memory successfully allocated using calloc.
    y = 0
    Last edited by Dadu@; 02-27-2022 at 12:25 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > program proves how calloc is different then malloc
    There's no guarantee that you'll never get x = 0 either.

    In fact, zero filled malloc is quite common because it eliminates one form of information leakage. Imagine if someone could steal your logins from your browser, just by allocating large amounts of memory and looking through it for tell-tale patterns.

    Also, this.
    Question 7.31
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2021
    Posts
    138
    Your code does not allocate 5 integers.

    Your code says "hey, allocator, can I have space for 2 integers please?"

    Then later your code says, "hey, allocator, you know this space you gave me for 2 integers? Can you increase it to 3 integers please?"

    realloc doesn't "add" it "grows." So if you want space for 5, you have to request space for 5. And if you're growing something, you probably want a variable to track the current capacity.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is Calloc in C?
    By muke786 in forum C Programming
    Replies: 12
    Last Post: 04-20-2020, 11:38 PM
  2. calloc
    By dunsta in forum C Programming
    Replies: 5
    Last Post: 05-05-2010, 08:31 AM
  3. re-calloc???
    By audinue in forum C Programming
    Replies: 6
    Last Post: 07-20-2008, 05:06 PM
  4. calloc
    By goran00 in forum C Programming
    Replies: 27
    Last Post: 04-07-2008, 12:50 PM
  5. Why use calloc()?
    By dwks in forum C Programming
    Replies: 8
    Last Post: 07-20-2005, 08:22 AM

Tags for this Thread