difference between calloc() and malloc()
difference between calloc() and malloc()
Collapse
This topic is closed.
X
X
-
manish sahuTags: None -
=?utf-8?Q?David_C=C3=B4me?=
Re: difference between calloc() and malloc()
On Wed, 13 Feb 2008 20:49:47 +0100, manish sahu <rocky_msit@yah oo.com>
wrote:
difference between calloc() and malloc()
RTFM
-
Christopher
Re: difference between calloc() and malloc()
On Feb 13, 1:49 pm, manish sahu <rocky_m...@yah oo.comwrote:You shouldn't be using either one in C++ if you can help it. Usedifference between calloc() and malloc()
operator new instead.
Comment
-
acehreli@gmail.com
Re: difference between calloc() and malloc()
On Feb 13, 11:56 am, Christopher <cp...@austin.r r.comwrote:new constructs objects but malloc and calloc reserve raw memory;On Feb 13, 1:49 pm, manish sahu <rocky_m...@yah oo.comwrote:
>>difference between calloc() and malloc()
You shouldn't be using either one in C++ if you can help it. Use
operator new instead.
different...
Ali
Comment
-
Victor Bazarov
Re: difference between calloc() and malloc()
acehreli@gmail. com wrote:Well, malloc and calloc are just 'new char[]' and 'new char[]()'.On Feb 13, 11:56 am, Christopher <cp...@austin.r r.comwrote:>>On Feb 13, 1:49 pm, manish sahu <rocky_m...@yah oo.comwrote:
>>>>>>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...
They don't "reserve raw memory" since there is no "raw memory" in
C++.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Comment
-
acehreli@gmail.com
Re: difference between calloc() and malloc()
On Feb 13, 12:42 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:With "raw memory," I meant the allocated storage that allocatorsacehr...@gmail. com wrote:>On Feb 13, 11:56 am, Christopher <cp...@austin.r r.comwrote:On Feb 13, 1:49 pm, manish sahu <rocky_m...@yah oo.comwrote:>>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...
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++.
allocate; you know, to construct objects on... I don't keep a copy of
the standard any more, but I'm pretty sure it talks about storage. I
regret now that I called that "raw memory"...
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.
Ali
Comment
-
acehreli@gmail.com
Re: difference between calloc() and malloc()
On Feb 13, 1:46 pm, acehr...@gmail. com wrote:
Even though I typed "allocates" twice above, I meant "constructs "On your point about using new[] for storage, it is misguided. Because
new char[] allocates uninitialized chars and new char[]() allocates
initialized chars.
twice. My point below is that new[] constructs chars.
AliThe 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.
Comment
-
Daniel T.
Re: difference between calloc() and malloc()
manish sahu <rocky_msit@yah oo.comwrote:
From http://www.dinkumware.com/manuals/default.aspxdifference between calloc() and malloc()
calloc
void *calloc(size_t nelem, size_t size);
The function allocates an array object containing nelem elements each of
size 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.
malloc
void *malloc(size_t size);
The function allocates an object of size size, and returns the address
of the object if successful; otherwise, it returns a null pointer.
See the difference?
Comment
-
James Kanze
Re: difference between calloc() and malloc()
On Feb 13, 9:42 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:acehr...@gmail. com wrote:On Feb 13, 11:56 am, Christopher <cp...@austin.r r.comwrote:On Feb 13, 1:49 pm, manish sahu <rocky_m...@yah oo.comwrote:>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...That's not true. Raw memory is what exists between the momentWell, malloc and calloc are just 'new char[]' and 'new
char[]()'. They don't "reserve raw memory" since there is no
"raw memory" in C++.
the memory is allocated, and the moment the constructor is run,
or between the moment the destructor finishes and the moment the
memory is freed. Call reserve() on an empty std::vector, and
that vector will contain raw memory.
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.
--
James Kanze (GABI Software) email:james.kan ze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Comment
-
Jeff Schwab
Re: difference between calloc() and malloc()
James Kanze wrote:
What are the problems with calloc? Googling "calloc errors" andThe 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.
"problems with calloc" did not turn up anything obvious.
Comment
-
Pete Becker
Re: difference between calloc() and malloc()
On 2008-02-14 13:09:06 -0500, Jeff Schwab <jeff@schwabcen ter.comsaid:
It sets all the bytes in the allocated memory to 0. All too often, thatJames Kanze wrote:
>>>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.
will mean that uninitialized data members look like they have
legitimate values.
--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
Comment
-
Jack Klein
Re: difference between calloc() and malloc()
On Thu, 14 Feb 2008 14:03:46 -0500, Pete Becker
<pete@versatile coding.comwrote in comp.lang.c++:
....as opposed to producing undefined value when the uninitialized dataOn 2008-02-14 13:09:06 -0500, Jeff Schwab <jeff@schwabcen ter.comsaid:
>>James Kanze wrote:
What are the problems with calloc? Googling "calloc errors" andThe 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.
"problems with calloc" did not turn up anything obvious.
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.
members are read to determine whether they are valid?
I'll admit that calloc() is not often useful in either C or C++, but
there are occasional times when it, even in C++.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
Comment
-
James Kanze
Re: difference between calloc() and malloc()
On Feb 14, 7:09 pm, Jeff Schwab <j...@schwabcen ter.comwrote:James Kanze wrote: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.Using uninitialized memory is an error. Calloc sets all of theWhat are the problems with calloc?
bytes to 0, which happens to correspond to zero initialization
on a lot of systems BUT is not necessarily the case. If your
code counts on zero initialization, using calloc will typically
mask the error (until you have to port to a machine where null
pointers aren't all 0 bits, or some such).
A more reasonable choice would be to initialize all of the
memory with some easily identifiable pattern which probably
won't work---0xDEADBEEF is a popular choice.
An even more reasonable choice, of course, is to use new:-).
--
James Kanze (GABI Software) email:james.kan ze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Comment
-
Ron Natalie
Re: difference between calloc() and malloc()
Victor Bazarov wrote:The are NOT just new char[].acehreli@gmail. com wrote:>>On Feb 13, 11:56 am, Christopher <cp...@austin.r r.comwrote:>new constructs objects but malloc and calloc reserve raw memory;>>On Feb 13, 1:49 pm, manish sahu <rocky_m...@yah oo.comwrote:
>>>
>>>difference between calloc() and malloc()
>>You shouldn't be using either one in C++ if you can help it. Use
>>operator new instead.
>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++.
>
V
They allocate chars from the C library free store.
The only difference with calloc is that it multiples the two args
together (this is one those attrocious carry over's from the so-called
portable IO library like the stdio funcs that should have never been
adopted by any sane language) and zeros the returned memory.
Your C++ allocation functions might allocate from the same arena, then
again they might not.
Comment
Comment