Like Tree1Likes
  • 1 Post By AndrewHunter

is calloc also contiguous like malloc

This is a discussion on is calloc also contiguous like malloc within the C Programming forums, part of the General Programming Boards category; hey guys, a certain thread confused me saying that malloc allocates memory contiguously but calloc DOES NOT allocate memory contiguously ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    18

    is calloc also contiguous like malloc

    hey guys, a certain thread confused me saying that
    malloc allocates memory contiguously but
    calloc DOES NOT allocate memory contiguously
    Is that correct ?

    I tried writing this program and deduced that CALLOC IS ALSO CONTIGUOUS
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    int main(void)
    {
     char* ptrm;
     char* ptrc;
     int i=0;
     
     ptrc=(char*)calloc(5,sizeof(char));
     printf("\nCalloced" );
     for(i=0;i<5;i++)
     {
      printf("\n%x",ptrc+i);
     }
    
     printf("\n\n");
    
     printf("\nMalloced ");
     ptrm=(char*)malloc(5*sizeof(char));
     for(i=0;i<5;i++)
     {
      printf("\n%x",ptrm+i);
     }
    
     
    
     getch();
     return 0;
    }
    Please correct me.

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Malloc and calloc both request memory from the heap in chunks. Calloc () is functionally equivalent to:
    Code:
    p = malloc(m*n);
    memset(p, 0, m*n);
    I am not sure what thread you are talking about with this contiguous allocation difference thing, but that thread would be wrong. Both functions allocate a chunk of memory from the heap. That's it, calloc just simply attempts to initialize that memory to all bits zero.

    Note that memory to all bits zero does not mean that it will initialize all values to zero. This is one of the reasons calloc isn't widely used. For more information on this I suggest reading:
    Calloc ()
    Null confusion

    EDIT: Please read this fact on casting malloc (Hint:same thing applies for calloc)
    Last edited by AndrewHunter; 07-16-2011 at 12:34 AM.
    rags_to_riches likes this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc and calloc
    By rrc55 in forum C Programming
    Replies: 15
    Last Post: 09-02-2009, 07:35 PM
  2. Malloc And Calloc
    By pradeepc in forum C Programming
    Replies: 1
    Last Post: 07-28-2007, 12:48 AM
  3. Malloc vs. Calloc
    By FCF in forum C Programming
    Replies: 13
    Last Post: 06-30-2002, 06:41 PM
  4. calloc vs malloc
    By Jubba in forum C Programming
    Replies: 2
    Last Post: 02-21-2002, 03:54 PM
  5. Calloc vs. Malloc
    By SavesTheDay in forum C Programming
    Replies: 3
    Last Post: 02-18-2002, 02:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21