I was wondering what calloc() is good for (besides setting everything to zero). As far as I can see,
is the same asCode:calloc(x, y);
(Except for calloc setting the allocated memory to zero.)Code:malloc(x*y);
So what is calloc() good for?
I was wondering what calloc() is good for (besides setting everything to zero). As far as I can see,
is the same asCode:calloc(x, y);
(Except for calloc setting the allocated memory to zero.)Code:malloc(x*y);
So what is calloc() good for?
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Like you said, for initializing the allocated memory to 0...Code:So what is calloc() good for?
If you wanted to you could live your entire life without using calloc() by just doing:
...instead.Code:void *ptr = malloc(SIZE); memset(ptr, 0, SIZE);
If you understand what you're doing, you're not learning anything.
That's what I thought . . . .
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
On some systems the calloc call could be optimized better for getting arrays of zeroed bytes. The system can keep a list of zeroed out memory and non-zeroed memory so calloc can look up memory in the zero pool while malloc returns pointers to the other pool and the memset is extra overhead that was already done on an idle moment for the zero pool.
Windows does that for instance. When the system is idle it starts the zero page thread. I don't know about calloc implementations that make use of it thuogh.
http://www.eskimo.com/~scs/C-faq/q7.31.html
Read the health warning.
Last edited by Salem; 07-20-2005 at 05:28 AM. Reason: typo
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.
So it initialises to zero not NULL?
> So it initialises to zero not NULL?
Yes, calloc is nothing more than malloc + memset - the result is a block of memory with all bits set to 0.
But nothing in C insists that NULL (as a pointer) has to be represented by all-bits-zero
http://www.eskimo.com/~scs/C-faq/s5.html
void *p = NULL;
if ( p == NULL )
if ( p == 0 )
These will always be true, whether your machine uses all-bits-zero for a NULL pointer or not.
void *p; memset( &p, 0, sizeof(void*) ); // just what calloc would do
if ( p == NULL )
if ( p == 0 )
These will work on some machines, and not on others.
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.
Do you know of any system where this is true btw? I'm not questioning the portability issue, just curious.Originally Posted by Salem
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*