Message-Id: Comments: Authenticated sender is From: "Salvador Eduardo Tropea (SET)" Organization: INTI To: Nathan Gray Date: Wed, 28 May 1997 17:46:43 +0000 MIME-Version: 1.0 Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7BIT Subject: Re: C or C++ CC: djgpp AT delorie DOT com Precedence: bulk Nathan Gray 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