delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/05/28/16:33:18

Message-Id: <m0wWjis-000S1vC@natacha.inti.edu.ar>
Comments: Authenticated sender is <salvador AT natacha DOT inti DOT edu DOT ar>
From: "Salvador Eduardo Tropea (SET)" <salvador AT inti DOT edu DOT ar>
Organization: INTI
To: Nathan Gray <gray AT ll DOT mit DOT edu>
Date: Wed, 28 May 1997 17:46:43 +0000
MIME-Version: 1.0
Subject: Re: C or C++
CC: djgpp AT delorie DOT com

Nathan Gray <gray AT ll DOT mit DOT edu> wrote:

> Since we're on the subject of C and C++, does anyone have a good
> explanation for the differences between malloc() and new?  As a native
> C++ programmer, my first instinct is to use new, but I see malloc() so
> often I'm starting to wonder if I'm missing something.

As the Dr. Andras stated new calls to the constructor, you won't get that from malloc. 
For something like:

new int[1000]

there is no difference with:

(int *)malloc(sizeof(int)*1000)

because gcc calls to an internal routine for new that calls to malloc.
A similar thing is for delete/free.
BUT!, 
1) This can be different in another compiler.
2) The first version is more easy and compact.
3) new and delete can be overloaded and have some special features, in this way 
you can change the behavior of the program without important changes (I used 
that on Borland to catch delete NULL cases).
4) The built in new and free of gcc have some extra checks (free(NULL) is hence 
checked twice ;-).

SET

> -- 
> | Nathaniel A. Gray	| MIT Lincoln Laboratory		   |
> | gray AT ll DOT mit DOT edu	| Group 35 - Signature Studies and Analysis|
> |------------------------------------------------------------------|
> | "But don't panic!  Base eight is just like base ten, really...   |
> |  if you're missing two fingers!"  -Tom Lehrer                    |
> 
> 
------------------------------------ 0 --------------------------------
Visit my home page: http://www.geocities.com/SiliconValley/Vista/6552/
Salvador Eduardo Tropea (SET). (Electronics Engineer)
Address: Curapaligue 2124, Caseros, 3 de Febrero
Buenos Aires, (1678), ARGENTINA
TE: +(541) 759 0013

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019