473,385 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

difference between calloc() and malloc()

difference between calloc() and malloc()
Feb 13 '08 #1
13 2464
On Wed, 13 Feb 2008 20:49:47 +0100, manish sahu <ro********@yahoo.com>
wrote:
difference between calloc() and malloc()

RTFM
Feb 13 '08 #2
On Feb 13, 1:49 pm, manish sahu <rocky_m...@yahoo.comwrote:
difference between calloc() and malloc()
You shouldn't be using either one in C++ if you can help it. Use
operator new instead.
Feb 13 '08 #3
On Feb 13, 11:56 am, Christopher <cp...@austin.rr.comwrote:
On Feb 13, 1:49 pm, manish sahu <rocky_m...@yahoo.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...

Ali
Feb 13 '08 #4
ac******@gmail.com wrote:
On Feb 13, 11:56 am, Christopher <cp...@austin.rr.comwrote:
>On Feb 13, 1:49 pm, manish sahu <rocky_m...@yahoo.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++.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 13 '08 #5
On Feb 13, 12:42 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
acehr...@gmail.com wrote:
On Feb 13, 11:56 am, Christopher <cp...@austin.rr.comwrote:
On Feb 13, 1:49 pm, manish sahu <rocky_m...@yahoo.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++.
With "raw memory," I meant the allocated storage that allocators
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
Feb 13 '08 #6
On Feb 13, 1:46 pm, acehr...@gmail.com wrote:
On your point about using new[] for storage, it is misguided. Because
new char[] allocates uninitialized chars and new char[]() allocates
initialized chars.
Even though I typed "allocates" twice above, I meant "constructs"
twice. My point below is that new[] constructs 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
Feb 13 '08 #7
manish sahu <ro********@yahoo.comwrote:
difference between calloc() and malloc()
From http://www.dinkumware.com/manuals/default.aspx

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?
Feb 14 '08 #8
On Feb 13, 9:42 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
acehr...@gmail.com wrote:
On Feb 13, 11:56 am, Christopher <cp...@austin.rr.comwrote:
On Feb 13, 1:49 pm, manish sahu <rocky_m...@yahoo.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++.
That's not true. Raw memory is what exists between the moment
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:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Feb 14 '08 #9
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.
What are the problems with calloc? Googling "calloc errors" and
"problems with calloc" did not turn up anything obvious.
Feb 14 '08 #10
On 2008-02-14 13:09:06 -0500, Jeff Schwab <je**@schwabcenter.comsaid:
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.

What are the problems with calloc? Googling "calloc errors" and
"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.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Feb 14 '08 #11
On Thu, 14 Feb 2008 14:03:46 -0500, Pete Becker
<pe**@versatilecoding.comwrote in comp.lang.c++:
On 2008-02-14 13:09:06 -0500, Jeff Schwab <je**@schwabcenter.comsaid:
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.
What are the problems with calloc? Googling "calloc errors" and
"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.
....as opposed to producing undefined value when the uninitialized data
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.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Feb 15 '08 #12
On Feb 14, 7:09 pm, Jeff Schwab <j...@schwabcenter.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.
What are the problems with calloc?
Using uninitialized memory is an error. Calloc sets all of the
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:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Feb 15 '08 #13
Victor Bazarov wrote:
ac******@gmail.com wrote:
>On Feb 13, 11:56 am, Christopher <cp...@austin.rr.comwrote:
>>On Feb 13, 1:49 pm, manish sahu <rocky_m...@yahoo.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++.

V
The are NOT just new char[].
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.
Feb 16 '08 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Koster | last post by:
Sorry for the re-post, but my previous question was left unanswered. I have a question about the appropriateness of calloc. Consider an array of pointers to structs which need to be allocated...
17
by: rihad | last post by:
To make up a proper dynamically allocated empty C string, would it suffice to say: return calloc(1, 1); or am I better off using the longer version char *p = malloc(1); if (p) *p = 0;...
27
by: MK | last post by:
I am a newbie. Please help. The following warning is issued by gcc-3.2.2 compiler (pc Linux): ================================================================== read_raw_data.c:51: warning:...
15
by: Mohanasundaram | last post by:
Hi All, What is the difference between malloc and calloc other than the point that calloc will initialize the memory to all zeros? This was an interview question for me. All the books and...
6
by: brian | last post by:
I went through my implementation's (BSD) source code for calloc(), and if no more than one object is being allocated, the code will still execute the following if() (from...
37
by: Harsimran | last post by:
Can any one explain what are far pointers and what is the difference between malloc and calloc .Which is better ?
11
by: lohith.matad | last post by:
Hi all, Though the purpose of both malloc() and calloc() is the same, and as we also know that calloc() initializes the alloacted locations to 'zero', and also that malloc() is used for bytes...
8
by: venkatesh | last post by:
hai to everybody, i am having doubt in difference between the malloc and calloc function. please tell where to use and when to use those functions?
6
by: mthread | last post by:
Hi, I am learning C++ and I have been told that an object can be created either by using calloc or new. If it is true, can some one tell me what is the difference b/w using these two function...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.