Article View: gnu.g++.bug
Article #896delete [size] pointer; Problem
From: Mark Rollins
Date: Thu, 03 Sep 1992 18:01
Date: Thu, 03 Sep 1992 18:01
80 lines
2716 bytes
2716 bytes
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:
-----------------------------------------------------------------
#include <Complex.h>
class Vector {
int size;
Complex *v;
public:
Vector(int s=1) { size = s; v = new Complex[size];};
~Vector() { delete [size] v;}
};
void foo(int s)
{
Vector junk(s);
}
main()
{
Vector* test;
for (int i=0;i<100000;i++)
foo(1000);
}
-----------------------------------------------------------------
Buggy version that quickly uses up all virtual memory:
(difference is in the ~Vector() destructor)
-----------------------------------------------------------------
#include <Complex.h>
class Vector {
int size;
Complex *v;
public:
Vector(int s=1) { size = s; v = new Complex[size];};
~Vector() { delete v;}
};
void foo(int s)
{
Vector junk(s);
}
main()
{
Vector* test;
for (int i=0;i<100000;i++)
foo(1000);
}
--
******************************************************************
* Mark Rollins |email: *
* Dept. Elec. Eng. | rollins@eleceng.ee.queensu.ca *
* Queen's University | *
* Kingston, Ontario, CANADA | *
* K7L-3N6 | *
******************************************************************
Technical Details
Message-ID:
<92Sep3.220137edt.30@jarvis.csri.toronto.edu>Path:
arc1-ca.pugleaf.net!sparky!uunet!wupost!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!bart.ee.queensu.ca!rollinsShow Full Headers
Complete Article Headers
Newsgroups: gnu.g++.bug Path: arc1-ca.pugleaf.net!sparky!uunet!wupost!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!bart.ee.queensu.ca!rollins From: rollins@bart.ee.queensu.ca (Mark Rollins) Subject: delete [size] pointer; Problem Message-ID: <92Sep3.220137edt.30@jarvis.csri.toronto.edu> Sender: rollins@bart.ai.mit.edu (Mark Rollins) Organization: Queen's University, Kingston Distribution: gnu Date: Thu, 3 Sep 1992 18:01:03 GMT Approved: bug-g++@prep.ai.mit.edu Lines: 81