Article View: gnu.g++.help
Article #180Re: PROBLEM DELETING A VECTOR OF CLASS OBJECTS
From: Bob Martin
Date: Fri, 04 Sep 1992 14:17
Date: Fri, 04 Sep 1992 14:17
41 lines
2216 bytes
2216 bytes
rollins@bart.ee.queensu.ca (Mark Rollins) writes:
|PROBLEM DELETING A VECTOR OF CLASS OBJECTS
|------------------------------------------
|Using the syntax of Stroustrup's 1st edition, pg. 162
|and g++ version 2.2.2, libg++ version 2.1, and SunOS 4.2.2 I get
|correct results but a compiler warning:
| g++ -o bin/junk badmem.cc -lg++ -lm
| badmem.cc: In method `Vector::~Vector ()':
| badmem.cc:9: warning: use of array size with vector delete is anachronistic
|If I omit the array size, the Vector does not get de-allocated
|properly using the destructor, and all virtual memory gets eaten up
|rather quickly. (It's probably just deleting the 1st one in the array).
|What is the proper syntax for deleting a vector of class objects ?
|Or is there any way to turn off this annoying warning flag ?
|-----------------------------------------------------------------
|Working version that produces a warning:
|-----------------------------------------------------------------
| ~Vector() { delete [size] v;}
|-----------------------------------------------------------------
|Buggy version that quickly uses up all virtual memory:
| (difference is in the ~Vector() destructor)
|-----------------------------------------------------------------
| ~Vector() { delete v;}
The correct syntax is "delete [] v;" The 'size' parameter is an
anachronism, but the brackets are not!
--
Robert Martin Training courses offered in:
R. C. M. Consulting Object Oriented Analysis
2080 Cranbrook Rd. Object Oriented Design
Green Oaks, Il 60048 (708) 918-1004 C++
Technical Details
Message-ID:
<rmartin.715616269@thor>Path:
arc1-ca.pugleaf.net!sparky!uunet!stanford.edu!ames!olivea!spool.mu.edu!sdd.hp.com!usc!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!thor.UUCP!rmartinReferences:
<Bu16wq.L6E@knot.ccs.queensu.ca>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!stanford.edu!ames!olivea!spool.mu.edu!sdd.hp.com!usc!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!thor.UUCP!rmartin From: rmartin@thor.UUCP (Bob Martin) Newsgroups: gnu.g++.help,comp.lang.c++ Subject: Re: PROBLEM DELETING A VECTOR OF CLASS OBJECTS Message-ID: <rmartin.715616269@thor> Date: 4 Sep 92 14:17:49 GMT References: <Bu16wq.L6E@knot.ccs.queensu.ca> Sender: gnulists@ai.mit.edu Followup-To: comp.lang.c++ Organization: Gatewayed from the GNU Project mailing list help-g++@prep.ai.mit.edu Lines: 41