Thread: Malloc vs. Calloc

  1. #1
    Registered User FCF's Avatar
    Join Date
    Dec 2001
    Posts
    40

    Malloc vs. Calloc

    Wat are the differences between Malloc() and Calloc()?

  2. #2
    Registered User whistlenm1's Avatar
    Join Date
    Jan 2002
    Posts
    124
    Hopes this will help

    void *malloc(size_t size);

    The function allocates an object of size_t size (size_t = int, char, float...etc), and returns the address of the object if successful; otherwise, it returns a null pointer. The values stored in the object are indeterminate. You can safely convert the return value to an object pointer of any type whose size is not greater than size.

    void *calloc(size_t nelem, size_t size);

    The function allocates an array object containing nelem (number of elements) each of size_t size, stores zeros in all bytes of the array, and returns the address of the first element of the array if successful; otherwise, it returns a null pointer. You can safely convert the return value to an object pointer of any type whose size in bytes is not greater than size.
    Man's mind once streched by a new idea, never regains its original dimensions
    - Oliver Wendell Holmes

    In other words, if you teach your cat to bark (output) and eat dog food (input) that doesn't make him a dog. It would have to chase cars, chew bones, and have puppies before I'd call it Rover ;-)
    - WaltP

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The first letter. The number of arugments each takes. Also, calloc zero-values all elements it allocates.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    malloc will return an uninitialized chunk of memory for you to use.
    calloc ( clear alloc ) will attempt to initialize that chunk of memory to all bits zero.

    Note that when I say attempt, I mean that this may not be possible so you shouldn't rely on it. Other than that the only difference is how they are called and how they are implemented. So in the end, which one you choose to use is a matter of preference.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User FCF's Avatar
    Join Date
    Dec 2001
    Posts
    40
    Thank u for all helps.

  6. #6
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    The final difference is calloc is almost never used. Even when you do need to zero out the structure most programmers will use memset or a macro that hides memset.

  7. #7
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Originally posted by quzah
    The first letter. The number of arugments each takes. Also, calloc zero-values all elements it allocates.
    Quzah.
    Does calloc zero-value value all its arguments , or does it clear all bytes to zero , for example it might not be neccessary that a double with all its sizeof(double) bytes zero represents the double value 0 (though it is true in all most all implementations) , for any integral type it should set all values to 0 .

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by pinko_liberal


    Does calloc zero-value value all its arguments , or does it clear all bytes to zero , for example it might not be neccessary that a double with all its sizeof(double) bytes zero represents the double value 0 (though it is true in all most all implementations) , for any integral type it should set all values to 0 .
    Not sure I understand what you're asking here.....

    calloc() and malloc() don't know what type of variable is going to be stored in the memory they are getting. They only know how much to allocate (in bytes).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Originally posted by Hammer

    Not sure I understand what you're asking here.....

    calloc() and malloc() don't know what type of variable is going to be stored in the memory they are getting. They only know how much to allocate (in bytes).
    What I meant was that all bits zero might not lead to a double with value 0 ,
    i.e,
    double *d;
    d=calloc(1, sizeof(double)) ;

    then it might not be necessary that
    ( *d == 0.) is true
    thought it will be true in almost all cases .
    Last edited by pinko_liberal; 07-07-2002 at 10:01 PM.

  10. #10
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Originally posted by pinko_liberal


    What I meant was that all bits zero might not lead to a double with value 0 ,
    i.e,
    double *d;
    d=calloc(1, sizeof(double)) ;

    then it might not be necessary that
    ( *d==0.) is true
    thought it will be true in almost all cases .
    I meant it almost all implementations .
    Last edited by pinko_liberal; 07-07-2002 at 10:03 PM.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Does calloc zero-value value all its arguments , or does it clear all bytes to zero , for example it might not be neccessary that a double with all its sizeof(double) bytes zero represents the double value 0 (though it is true in all most all implementations) , for any integral type it should set all values to 0 .
    Calloc clears the memory to all bits zero. You apparently understand why calloc is rarely used and not considered portable. All bits zero doesn't necessarily mean zero values so calloc is a subtle but dangerous trap to fall into. Not many people will make this distinction and happily write broken code thinking that calloc always clears the values to zero accordingly.

    -Prelude
    My best code is written with the delete key.

  12. #12
    Unregistered
    Guest
    How can all bits being set to 0 not give you a zero as the value? I mean if we convert the binary number with all 0's at every bit to decimal, then you will get a 0 for the decimal equivalent. Can someone explain how you can get something other than 0 when all bits are set to 0?

  13. #13
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    i'm a bit confused on that too

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >i'm a bit confused on that too
    Don't get confused about integers and chars, all bits zero is a zero value, but pointers and floating-point types are not guaranteed by the standard to be so because it would cause unnecessary constraints on machines that use non-zero bit patterns for such values.

    I highly recommend taking advantage of Steve Summit's extensive knowledge.
    http://www.eskimo.com/~scs/C-faq/q7.31.html
    http://www.eskimo.com/~scs/C-faq/q5.16.html

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc, calloc from the FAQ
    By salvadoravi in forum C Programming
    Replies: 10
    Last Post: 01-21-2008, 03:29 AM
  2. Malloc & Calloc difference in terms of memory allocated
    By swapnaoe in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 12:57 AM
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. difference between calloc and malloc
    By saravanan_ts in forum C Programming
    Replies: 4
    Last Post: 07-28-2003, 06:13 AM