473,805 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

difference between calloc() and malloc()

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

RTFM
Feb 13 '08 #2
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.
Feb 13 '08 #3
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...

Ali
Feb 13 '08 #4
ac******@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++.

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...@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...

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********@yah oo.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...@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...
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 objektorientier ter Datenverarbeitu ng
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

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

Similar topics

5
3646
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 space on the heap, for example: typedef struct myStruct *PMYSTRUCT; struct myStruct { int i; int j;
17
4896
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; return p;
27
4773
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: assignment makes pointer from integer without a cast ================================================================== when the following piece of code was compiled. The offending statement is calloc. A similar statement in the main() function...
15
2249
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 references that I have come across tells that calloc initialize the memory to all zeros. In Herbert Shiltd's C/C++ reference I found that calloc returns a pointer to an array. I want to understand in what way the memory returned by malloc and calloc...
6
4101
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 /usr/src/lib/libc/stdlib/calloc.c): if (num && size && SIZE_T_MAX / num < size) { errno = ENOMEM; return NULL; }
37
2586
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
5801
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 allocation whereas calloc() for chunk of memory allocation. Apart from these is there any strong reason that malloc() is prefered over calloc() or vice-versa? Looking forward for your clarrifications , possibly detailed.
8
11184
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
4930
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 calls.
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10359
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10104
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9182
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7645
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5541
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3843
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.