On Friday, February 4, 2011 at 16:45:00 (-0800) Alan W. Irwin writes:
> On 2011-02-04 14:30-0800 Alan W. Irwin wrote:
>
> > Anyone care to have a go at explaining the difference in meaning between
> >
> > delete text[k];
> >
> > and
> >
> > delete [] text[k];
> >
> > ?
> >
> > Both compile, run, and produce good results; but the former annoys
> > valgrind (and may actually do bad things) while the latter does not.
>
> To answer my own question (in part) from
> http://www.cplusplus.com/doc/tutorial/dynamic/, the first form deletes
> memory "allocated for a single element, and the second one for memory
> allocated for arrays of elements".
>
> However, I must say I don't understand why C++ implements new for
> single quantities and also has a special syntax for deleting those new
> single quantities. Is there some advantage to using "new" and delete
> for single quanties compared to just using ordinary automatic
> variables? I hope someone who understands C++ will enlighten me.
The issue arises because it's not a single quantity, it's an array of char.
The original allocation looks like:
for ( k = 0; k < MAX_NLEGEND; k++ )
text[k] = new char[200];
so the:
delete [] text[k];
is needed to delete the array of chars at location text[k]. If instead you
had:
text[k] = new <thing>;
where <thing> is either a primitive type or a first-class object, then you'd
just do:
delete text[k];
Maurice
------------------------------------------------------------------------------
The modern datacenter depends on network connectivity to access resources
and provide services. The best practices for maximizing a physical server's
connectivity to a physical network are well understood - see how these
rules translate into the virtual world?
http://p.sf.net/sfu/oracle-sfdevnlfb
_______________________________________________
Plplot-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/plplot-devel