home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c++:13283 gnu.g++.help:1187 gnu.g++.bug:1425
- Path: sparky!uunet!gatech!rpi!bu.edu!ai-lab!prep.ai.mit.edu!gnulists
- From: rollins@bart.ee.queensu.ca (Mark Rollins)
- Newsgroups: comp.lang.c++,gnu.g++.help,gnu.g++.bug
- Subject: (none)
- Message-ID: <Bu16wq.L6E@knot.ccs.queensu.ca>
- Date: 4 Sep 92 02:00:25 GMT
- Followup-To: comp.lang.c++,gnu.g++.help,gnu.g++.bug
- Organization: Queen's University, Kingston
- Lines: 80
- Approved: info-gnu@prep.ai.mit.edu
- To: bug-g++@prep.ai.mit.edu
-
-
- 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 | *
- ******************************************************************
-