delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/07/11/17:46:49

From: sparhawk AT eunet DOT at (Gerhard Gruber)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: "delete" and "delete []" operators
Date: Sat, 11 Jul 1998 21:29:20 GMT
Organization: Customer of EUnet Austria
Message-ID: <35a7cd29.26263473@news.Austria.EU.net>
References: <199807092012 DOT QAA25515 AT delorie DOT com> <35a71a7e DOT 2075466 AT news DOT Austria DOT EU DOT net> <35a7423b DOT 178710164 AT news DOT snafu DOT de>
NNTP-Posting-Host: e245.dynamic.vienna.at.eu.net
Mime-Version: 1.0
Lines: 61
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Destination: horst DOT kraemer AT snafu DOT de (Horst Kraemer)
From: Gruber Gerhard
Group: comp.os.msdos.djgpp
Date: Sat, 11 Jul 1998 11:28:36 GMT:

>After
>
>	class X;
>	typedef X Xa[10];
>
>	X* p = new Xa;
>
>you have to delete p via 'delete [] p' (!). This a case where 'new xa'
>does _not_ return a pointer to the "type xa" but to the base type of
>the xa, i.e. the above statement is equivalent to

That's good to know. This is one of the reasons why I rarely use typedefs to
cover complex definitions. It is quite logical that you'd have to use delete[]
in this case, because the compiler doesn't really create a new type. Reading
code written like this you would think that this would be an error unless you
know exactly what the compiler does and you are looking up the exact
definition. If you have a function like this and you don't know the defintion
this is quite confusing to understand when you only read the code.
f()
{
   X *p = NULL;
  ...
  p = new Xa;
  ...
  if(p)
     delete[]p;
}


>while 'new X' and 'new X[10]' will both return a 'pointer to X'.
>Typedefs are weird, aren't they ? ;-)

 :)Yes. Quite tough.  :) That's why I despise using typedefs too freely. It's
quite good to define basic types, but it's really bad reading code you didn't
write if there are complex defintions involved. At my last job we had a
programmer that used a typed for **. In the include there was "typedef x
**char"
In the actual code this was not visible unless you read the include file to
and it gave me quite some headache until I discovered that.

--
Bye,
   Gerhard

email: sparhawk AT eunet DOT at
       g DOT gruber AT sis DOT co DOT at

Spelling corrections are appreciated.

- Raw text -


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