M
manish sahu
difference between calloc() and malloc()
difference between calloc() and malloc()
You shouldn't be using either one in C++ if you can help it. Use
operator new instead.
new constructs objects but malloc and calloc reserve raw memory;
different...
new constructs objects but malloc and calloc reserve raw memory;
different...
Well, malloc and calloc are just 'new char[]' and 'new char[]()'.
They don't "reserve raw memory" since there is no "raw memory" in
C++.
On your point about using new[] for storage, it is misguided. Because
new char[] allocates uninitialized chars and new char[]() allocates
initialized chars.
The behaviour is quite different than allocating
storage to construct objects on. It is possible to construct object on
tops of chars, but it's a side effect of chars having trivial
destructors.
manish sahu said:difference between calloc() and malloc()
Well, malloc and calloc are just 'new char[]' and 'new
char[]()'. They don't "reserve raw memory" since there is no
"raw memory" in C++.
James said:The way raw memory is usually allocated in C++ is by calling the
operator new() function. malloc() can also be used. calloc()
should probably be avoided, both in C and in C++, because it
masks a number of serious errors.
What are the problems with calloc? Googling "calloc errors" and
"problems with calloc" did not turn up anything obvious.
Pete said:It sets all the bytes in the allocated memory to 0. All too often, that
will mean that uninitialized data members look like they have legitimate
values.
Jeff said:Huh? That's not only the documented behavior, it's the primary reason
to use calloc. I specifically have used it in the past to avoid
having to zero-out memory manually, which I do to make sure that the
program's behavior is at least deterministic. This is considered a
bad thing?
Victor said:I believe what Pete refers to is, if you forget to initialise
a member to something meaningful, then the use of 'calloc' when
allocating your class object dynamically would hide the fact
that you didn't initialise that member, and instead create
a false sense of security. When an object of the same type is
instantiated in automatic storage (local object, argument), the
uninitialised member will play some role, possibly detrimental,
and the bug would be difficult to track.
Jeff said:[..] I don't see the case of manually
requested, dynamically allocated memory as being so special that
garbage values are, in this one case, preferable to zeros.
Victor said:Jeff said:[..] I don't see the case of manually
requested, dynamically allocated memory as being so special that
garbage values are, in this one case, preferable to zeros.
I don't have any numbers, but the common sense suggests that
initialising to "all bytes zero" is slower than *not* doing it.
So, when you're allocating "raw memory" (OK, I'll concede there
is such a thing) for classes that do have their own constructors
which supposedly initialise the members as needed, would be
better done by malloc than calloc because (a) it's faster and
(b) it does not hide any errors due to missed initialisers.
I believe we are not talking about arrays of primitive types
here.
It sets all the bytes in the allocated memory to 0. All too often, that
will mean that uninitialized data members look like they have
legitimate values.
What are the problems with calloc?
Thank you, that makes some sense. I guess I'm at odds with the
prevailing wisdom here, though. I still prefer this:
auto primitive_t a[size] = { 0 };
To that:
auto primitive_t b[size];
If a local vector is used rather than an array (or a calloc'd block of
memory), I'm not even sure how to specify that the memory should be left
uninitialized...
auto std::vector<primitive_t> v(size, ?);
AFAIK, static objects are guaranteed to be zero'd by all conformant
implementations, too.
James said:Thank you, that makes some sense. I guess I'm at odds with the
prevailing wisdom here, though. I still prefer this:auto primitive_t a[size] = { 0 };To that:auto primitive_t b[size];
Yes, but that's not what calloc does. Calloc sets all of the
bits in the memory to 0, regardless of the type. Consider
something like:
void* a[ size ] = {0} ;
This initializes the array with null pointers. Calloc only
initializes the allocated memory with null pointers IF a null
pointer is represented by all bits 0, which while frequent,
isn't necessarily the case. So if you test on a machine where
null pointers are all bits 0, you have the impression that the
code works, even though you haven't correctly initialized the
memory.
If a local vector is used rather than an array (or a calloc'd block of
memory), I'm not even sure how to specify that the memory should be left
uninitialized...auto std::vector<primitive_t> v(size, ?);
You can't. std::vector does not allow uninitialized objects.
It can't---an uninitialized object is not copiable (unless it is
of character type).
AFAIK, static objects are guaranteed to be zero'd by all conformant
implementations, too.
They are guaranteed to be "zero initialized". That doesn't
necessarily mean all bits 0.
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.