PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > difference b/w using calloc and new
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
difference b/w using calloc and new

Réponse
 
LinkBack Outils de la discussion
Vieux 11/12/2007, 09h47   #1
mthread
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut difference b/w using calloc and new

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.



  Réponse avec citation
Vieux 11/12/2007, 09h57   #2
Jonathan Mcdougall
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: difference b/w using calloc and new

On Dec 11, 4:47 am, mthread <rjk...@gmail.com> wrote:
> 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.


Please, see

http://www.parashift.com/c++-faq-lit....html#faq-16.4

Browsing the whole FAQ might also be a good idea.

--
Jonathan Mcdougall
  Réponse avec citation
Vieux 12/12/2007, 08h19   #3
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: difference b/w using calloc and new

On Dec 11, 10:47 am, mthread <rjk...@gmail.com> wrote:

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


You've been told wrong. You can't create an object with calloc,
only allocate memory. (In practice, even in C, I've never found
a use for calloc.)

--
James Kanze (GABI Software) email:james.kanze@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

  Réponse avec citation
Vieux 12/12/2007, 08h47   #4
Alf P. Steinbach
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: difference b/w using calloc and new

* James Kanze:
> On Dec 11, 10:47 am, mthread <rjk...@gmail.com> wrote:
>
>> 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.

>
> You've been told wrong. You can't create an object with calloc,
> only allocate memory. (In practice, even in C, I've never found
> a use for calloc.)


Depends what's meant by "object". In the usual language-independent
meaning of "object" you're right. However, the standard defines an
object as a region of memory, and goes to pains to specify "class type"
whenever it talks about objects of class type, so as not to confuse them
with objects of non-class type, and in that meaning malloc & friends
allocate objects (although I agree that's probably not what OP means).

Now, C++ "new" combines two operations: allocation and initialization,
where initialization might involve internally calling a constructor for
each class type object created. You get either both effects, or none.
I.e., if initialization fails, the allocated memory is deallocated
again, automatically.

And it has one more useful property, namely that the corresponding
destruction+deallocation operation, C++ "delete", is the operation used
by default by automatic cleanup such as with boost::shared_ptr.

Implementing all that yourself on top of e.g. malloc would be very much
work for no particular gain, since it's unlikely in the extreme that
you'd be able to achieve the same quality.

Hence the only reason for using malloc & the like in C++, is where you
need to interface to code that requires this, e.g. by calling free.


Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
  Réponse avec citation
Vieux 12/12/2007, 08h51   #5
vairavans@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: difference b/w using calloc and new

On Dec 11, 2:47 pm, mthread <rjk...@gmail.com> wrote:
> 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.


new is not a function or a library, it is just an operator...

calloc just allocates the memory, where as new allocates the memory
for the object and also calls the constructor to perform
initialization.
yes calloc does set the memory contents with 0, however new perform's
user defined initialization (if provided).
  Réponse avec citation
Vieux 12/12/2007, 10h12   #6
mthread
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: difference b/w using calloc and new

On Dec 12, 1:51 pm, vairav...@gmail.com wrote:
> On Dec 11, 2:47 pm, mthread <rjk...@gmail.com> wrote:
>
> > 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.

>
> new is not a function or a library, it is just an operator...
>
> calloc just allocates the memory, where as new allocates the memory
> for the object and also calls the constructor to perform
> initialization.
> yes calloc does set the memory contents with 0, however new perform's
> user defined initialization (if provided).



Hi,
thanx every one for the .
  Réponse avec citation
Vieux 12/12/2007, 12h32   #7
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: difference b/w using calloc and new

On Dec 12, 9:47 am, "Alf P. Steinbach" <al...@start.no> wrote:
> * James Kanze:
> > On Dec 11, 10:47 am, mthread <rjk...@gmail.com> wrote:


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


> > You've been told wrong. You can't create an object with calloc,
> > only allocate memory. (In practice, even in C, I've never found
> > a use for calloc.)


> Depends what's meant by "object". In the usual
> language-independent meaning of "object" you're right.
> However, the standard defines an object as a region of memory,
> and goes to pains to specify "class type" whenever it talks
> about objects of class type, so as not to confuse them with
> objects of non-class type, and in that meaning malloc &
> friends allocate objects (although I agree that's probably not
> what OP means).


Yes, but even a POD is more than just raw memory, at least if it
has a readable value. Using calloc, you're guaranteed that any
subobjects of integral type have a value of 0, but that's about
it. You still have to initialize pointers and floating point
values before you can access them.

> Now, C++ "new" combines two operations: allocation and
> initialization, where initialization might involve internally
> calling a constructor for each class type object created. You
> get either both effects, or none. I.e., if initialization
> fails, the allocated memory is deallocated again,
> automatically.


Most importantly, initialization takes place. Conceptually (in
my mind, at least), there is a difference between raw memory,
and an object, even if that object is of type int. With
calloc/malloc (and the operator new function), you get raw
memory; with new, you get the object. If the constructor is a
no-op (a trivial constructor), of course, there is no real
difference physically, but I still find it cleaner to think of
it as two different things.

> And it has one more useful property, namely that the
> corresponding destruction+deallocation operation, C++
> "delete", is the operation used by default by automatic
> cleanup such as with boost::shared_ptr.


It's the only operation used by most automatic cleanup. (It's
what std::auto_ptr uses, for example.) Except
that which works at a much lower level (e.g. garbage
collection).

The important point, of course, is that if you use calloc, you
must use free (and not delete), and if you use new, you must use
delete. Unless you're using some lower level system which more
or less "pirates" them all.

> Implementing all that yourself on top of e.g. malloc would be
> very much work for no particular gain, since it's unlikely in
> the extreme that you'd be able to achieve the same quality.


It's useful when you want to separate initialization and
allocation, e.g. when implementing something like vector.
Today, of course, unless you're implementing the standard
library, the probability of needing to do this is very, very
small, but back in the old days, when the "standard library" was
iostream, complex, and nothing else, it was standard practice
anytime you implemented your in house vector classes.

Even then, I would use the operator new function, rather than
malloc. I reserve malloc for the implementation of custom
operator new functions (e.g. for debugging---although now that
tools like valgrind are readily available for free, there's
practically no reason to write a debugging operator new anymore
either).

> Hence the only reason for using malloc & the like in C++, is
> where you need to interface to code that requires this, e.g.
> by calling free.


In which case, you have no choice. When you have to, you have
to. Similarly, it's the easiest way (and the only portable way)
to get memory if you're implementing your own replacement global
operator new.

--
James Kanze (GABI Software) email:james.kanze@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
  Réponse avec citation
Vieux 12/12/2007, 12h34   #8
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: difference b/w using calloc and new

On Dec 12, 9:51 am, vairav...@gmail.com wrote:
> On Dec 11, 2:47 pm, mthread <rjk...@gmail.com> wrote:


[...]
> yes calloc does set the memory contents with 0, however new perform's
> user defined initialization (if provided).


You have to be very careful how you say that. Calloc only sets
all of the bits to 0. It does *not* guarantee that the value
of the objects (even POD objects) corresponds to initialization
with 0. In particular, there is absolutely no guarantee that
pointers in memory returned by calloc are null, or that floating
point values are 0.0.

--
James Kanze (GABI Software) email:james.kanze@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

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h14.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,18440 seconds with 16 queries