delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/05/28/17:36:58

From: jhw AT wetware DOT com (strychnine)
Newsgroups: alt.msdos.programmer,comp.os.msdos.djgpp,rec.games.programmer
Subject: Re: C or C++
Date: Wed, 28 May 1997 12:58:27 -0800
Organization: Dr. Strychnine's Box of Sharpened Springs
Message-ID: <jhw-2805971258270001@kallisti.warpspeedcom.com>
References: <5lrt2d$qun AT bambam DOT soi DOT city DOT ac DOT uk> <5mftso$2nc AT butch DOT lmms DOT lmco DOT com> <338C31B9 DOT 3146 AT ll DOT mit DOT edu>
NNTP-Posting-Host: 199.108.64.194
Mime-Version: 1.0
Lines: 21
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

In article <338C31B9 DOT 3146 AT ll DOT mit DOT edu>, 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.

new/delete invoke constructors and [virtual] destructors.  malloc/free
don't.  so you lose type safety by using malloc/free.

on the other hand, malloc/free are compatible with realloc.  new/delete
aren't.  so it makes sense to use malloc/free when working with arrays of
builtins that you intend to dynamically resize with realloc.

new/delete are operators.  malloc/free are extern "C" functions.  this
difference is... subtle... and has to do with overloading.

-- 
juliet hotel whiskey at whiskey echo tango whiskey alfa romeo echo
[sgi|mips|daver|indetech]!wetware!jhw
"Perhaps today is not such a good day to die." --Ambrose MacClaine

- Raw text -


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