delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/08/07/12:01:51

From: Erik Max Francis <max AT alcyone DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: new and delete
Followup-To: comp.os.msdos.djgpp,comp.lang.c++
Date: Fri, 07 Aug 1998 08:42:28 -0700
Organization: Alcyone Systems
Lines: 40
Message-ID: <35CB2064.2694236A@alcyone.com>
References: <35CAB780 DOT 4FB1 AT netvision DOT net DOT il>
NNTP-Posting-Host: charmaine.alcyone.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Guy Rauscher wrote:

> I'm sorry if this is the wrong group for this, but I couldn't find any
> more appropriate one.

Might want to have tried comp.lang.c++.

> My question is this: Why can't delete detect if its deleting an array
> or
> a single variable, like free() does? I don't always know how long is
> the
> array I'm deleting; it could be just one variable long, so should I
> use
> delete with or without []? Or maybe it doesn't matter anymore in new
> compilers?

It theoretically could, but it would require overhead on each use of
delete to determine whether or not the object was an array or an object.

Consider:

    int *pi = new int;    // create a new int
    int *ai = new int[5]; // create an array of int

The type of these two variables is exactly the same -- pointer to int. 
From the context, delete would have to figure out whether it had
originally allocated an object or an array of objects, because using the
same form of delete doesn't make a distinction:

    delete pi; // delete the int
    delete ai; // wrong -- should be delete[] ai

-- 
Erik Max Francis / email max AT alcyone DOT com / whois mf303 / icq 16063900 
Alcyone Systems / irc maxxon (efnet) / finger max AT sade DOT alcyone DOT com
  San Jose, CA / languages En, Eo / web http://www.alcyone.com/max/
          USA / icbm 37 20 07 N 121 53 38 W / &tSftDotIotE
             \
            / I've got the fever for the flavor of a cracker
           / Ice Cube

- Raw text -


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