Wat are the differences between Malloc() and Calloc()?
Wat are the differences between Malloc() and Calloc()?
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
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.
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.
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.
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 .Originally posted by quzah
The first letter. The number of arugments each takes. Also, calloc zero-values all elements it allocates.
Quzah.
Not sure I understand what you're asking here.....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 .
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]
What I meant was that all bits zero might not lead to a double with value 0 ,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).
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.
I meant it almost all implementations .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 .
Last edited by pinko_liberal; 07-07-2002 at 10:03 PM.
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.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 .
-Prelude
My best code is written with the delete key.
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?
>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.