Mail Archives: djgpp/1998/07/11/17:46:49
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 -