home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 13283 < prev    next >
Encoding:
Text File  |  1992-09-03  |  2.6 KB  |  94 lines

  1. Xref: sparky comp.lang.c++:13283 gnu.g++.help:1187 gnu.g++.bug:1425
  2. Path: sparky!uunet!gatech!rpi!bu.edu!ai-lab!prep.ai.mit.edu!gnulists
  3. From: rollins@bart.ee.queensu.ca (Mark Rollins)
  4. Newsgroups: comp.lang.c++,gnu.g++.help,gnu.g++.bug
  5. Subject: (none)
  6. Message-ID: <Bu16wq.L6E@knot.ccs.queensu.ca>
  7. Date: 4 Sep 92 02:00:25 GMT
  8. Followup-To: comp.lang.c++,gnu.g++.help,gnu.g++.bug
  9. Organization: Queen's University, Kingston
  10. Lines: 80
  11. Approved: info-gnu@prep.ai.mit.edu
  12. To: bug-g++@prep.ai.mit.edu
  13.  
  14.  
  15. PROBLEM DELETING A VECTOR OF CLASS OBJECTS
  16. ------------------------------------------
  17.  
  18. Using the syntax of Stroustrup's 1st edition, pg. 162
  19. and g++ version 2.2.2, libg++ version 2.1, and SunOS 4.2.2 I get
  20. correct results but a compiler warning:
  21.  
  22.    g++ -o bin/junk badmem.cc -lg++ -lm
  23.    badmem.cc: In method `Vector::~Vector ()':
  24.    badmem.cc:9: warning: use of array size with vector delete is anachronistic
  25.  
  26. If I omit the array size, the Vector does not get de-allocated
  27. properly using the destructor, and all virtual memory gets eaten up
  28. rather quickly. (It's probably just deleting the 1st one in the array).
  29.  
  30. What is the proper syntax for deleting a vector of class objects ?
  31. Or is there any way to turn off this annoying warning flag ?
  32.  
  33. -----------------------------------------------------------------
  34. Working version that produces a warning:
  35. -----------------------------------------------------------------
  36. #include <Complex.h>
  37.  
  38. class Vector {
  39.   int        size;
  40.   Complex    *v;
  41.  public:
  42.   Vector(int s=1) { size = s; v = new Complex[size];};
  43.   ~Vector() { delete [size] v;}
  44. };
  45.  
  46. void foo(int s)
  47. {
  48.   Vector junk(s);
  49. }
  50.  
  51. main()
  52. {
  53.   Vector* test;
  54.   for (int i=0;i<100000;i++) 
  55.     foo(1000);
  56. }
  57. -----------------------------------------------------------------
  58. Buggy version that quickly uses up all virtual memory:
  59.   (difference is in the ~Vector() destructor)
  60. -----------------------------------------------------------------
  61.  
  62. #include <Complex.h>
  63.  
  64. class Vector {
  65.   int        size;
  66.   Complex    *v;
  67.  public:
  68.   Vector(int s=1) { size = s; v = new Complex[size];};
  69.   ~Vector() { delete v;}
  70. };
  71.  
  72. void foo(int s)
  73. {
  74.   Vector junk(s);
  75. }
  76.  
  77. main()
  78. {
  79.   Vector* test;
  80.   for (int i=0;i<100000;i++) 
  81.     foo(1000);
  82. }
  83.  
  84.  
  85. -- 
  86.  
  87. ******************************************************************
  88. *  Mark Rollins                |email:                           *
  89. *  Dept. Elec. Eng.            |  rollins@eleceng.ee.queensu.ca  *
  90. *  Queen's University          |                                 *
  91. *  Kingston, Ontario, CANADA   |                                 *    
  92. *  K7L-3N6                     |                                 * 
  93. ******************************************************************
  94.