D
deepu
Please reply...
Please reply...
deepu said:What is the advantage of malloc over calloc.
i.e. since malloc exists
to perform the memory operations why calloc is needed.
Asked in an interview.
Do you know the difference between the two? Hint, one zeros thedeepu said:What is the advantage of malloc over calloc. i.e. since malloc exists
to perform the memory operations why calloc is needed. Asked in an
interview.
Ian Collins said:Do you know the difference between the two? Hint, one zeros the
allocated memory, which may or may not be an advantage.
Keith Thompson said:Keep in mind that zeroing memory won't necessarily do anything useful
for pointers or floating-point objects. (Worse yet, it very commonly
will set them to NULL and 0.0, respectively, which can make certain
bugs very difficult to find.)
Yes, your program may not *want* NULLs and
0.0s, but knowing what they are, and why they were set to that value,
must be better than the uninitialized contents provided by malloc(). ?
Keep in mind that zeroing memory won't necessarily do anything useful
for pointers or floating-point objects. (Worse yet, it very commonly
will set them to NULL and 0.0, respectively, which can make certain
bugs very difficult to find.)
values,Why? If your program has no bugs, then you will not use these
so it makes no difference what they are. If your program does have
bugs, then random values may well cause the error to be spotted sooner
than null pointers and 0.0 floats.
A real example: when shared libraries were added to SunOS, several
programs (standard unix commands) were found that had been unwittingly
relying on automatic variables being initialised to zero. This showed
up because the dynamic loading used the stack before main() was
called.
Why? If your program has no bugs, then you will not use these values,
so it makes no difference what they are. If your program does have
bugs, then random values may well cause the error to be spotted sooner
than null pointers and 0.0 floats.
A real example: when shared libraries were added to SunOS, several
programs (standard unix commands) were found that had been unwittingly
relying on automatic variables being initialised to zero. This showed
up because the dynamic loading used the stack before main() was
called.
Some malloc implementations have an option to "scribble" over memory
before mallocing and after freeing, to increase the likelihood of
errors being detected.
Richard said:Could you expand on this a little. How on earth can it not be useful
to zero a block of memory if that is the intent?
What do you mean by
"it will commonly set then to NULL"? of course it would : its zeroing
memory (e.g zeroing a block of char **).
Because it's not /guaranteed/ to do so for non-integer types.
Thanks for the reply Richard (and an interesting SunOS example).
I'm not comfortable with the approach that assuming, or relying upon,
random values *may* trigger bugs in code. I understand why this may
be effective, as you've described, but surely calling calloc() could
be more effective in helping to isolate one of those bugs in the first
place, by provided a known state from which to start that debugging.
One could also argue that setting pointers to NULL values is more likely
to trigger unknown pointer bugs, than if they just had random values
where 1 in 4, or 1 in 8, may just work.
Richard said:I have no idea how this came up : when I said "intent" clearly the
intent (with the example) was to zero banks of pointers. I agree that
for non integer types that 0s dont necessarily compute to 0..
To sum up : of course calloc is useful
There is no requirement that the bytes of a null pointer be zeroes.
A program that assumes that there is - because it's been tried on
machines where null pointers have zero bytes - will likely break
when it encounters an implementation where null pointers have interesting
bit-patterns.
[Note that the standard /does/ require that a null pointer compare equal
to a literal 0; this is not the same case.]
Richard G. Riley said:The only issue being (and I'm not sure how it was raised since it
seemd a bit of a strawman to knock calloc) that zeroed memory isnt't
always what might be intended if real "0" values were intended :
although more theory than practice in real world IMO since one of the
only reasons I would use calloc would be to initialise pointer blocks.
(good to have a sensible discussion).
I know it's all about standard C here, and that history can't be changed,
but I'd suggest that much C programming may be 'easier' if the default
function of choice was calloc(), which always zeroes memory, and for those
that require it, a function named randalloc() be available which fills
memory with random, or hazardous, bytes (if memset() is insufficient).
Richard said:Makes me wonder about the
usefulness of calloc after all.
Richard said:There is no requirement that the bytes of a null pointer be zeroes.
A program that assumes that there is - because it's been tried on
machines where null pointers have zero bytes - will likely break
when it encounters an implementation where null pointers have interesting
bit-patterns.
[Note that the standard /does/ require that a null pointer compare equal
to a literal 0; this is not the same case.]
I must say that in many years of programming it is the first time I
have come across this. I stand corrected. Makes me wonder about the
usefulness of calloc after all.
Do you know of a platform where "Null" pointers did not cast to zero?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.