home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16338 < prev    next >
Encoding:
Text File  |  1992-11-15  |  2.5 KB  |  87 lines

  1. Path: sparky!uunet!cs.utexas.edu!sun-barr!olivea!spool.mu.edu!wupost!waikato.ac.nz!comp.vuw.ac.nz!newshost!srlncnc
  2. From: srlncnc@gopher.dosli.govt.nz (Chris Crook)
  3. Newsgroups: comp.lang.c++
  4. Subject: Using delete with arrays - help please!
  5. Message-ID: <1992Nov16.023852.24205@gopher.dosli.govt.nz>
  6. Date: 16 Nov 92 02:38:52 GMT
  7. Sender: srlncnc@gopher.dosli.govt.nz (Chris Crook)
  8. Organization: Department of Survey and Land Information, New Zealand
  9. Lines: 76
  10.  
  11. Hi
  12.  
  13. Can you clear up a bit of confusion I have about the use of delete with
  14. arrays.
  15.  
  16. My understanding is that I can allocate an array of class instances using
  17. the syntax
  18.  
  19.    MyClass arrayOfInstances = new MyClass[arraySize];
  20.  
  21. and that when I want to delete it I use
  22.  
  23.    delete [] arrayOfInstances
  24.  
  25. I have written a small test program to let me know when the constructors
  26. and destructors are called, and this all seems to work.  If I omit the
  27. [] in the delete statement then the destructor is called only once.  It
  28. also leads to a "Null pointer assignment" error from Borland C++ when the
  29. program ends, presumably because the memory allocation has got messed
  30. up.
  31.  
  32. What I am puzzled about is the wide use of arrays of characters in
  33. the Borland code examples in which the arrays are allocated as
  34.  
  35.    char *aBuffer = new char[bufferSize];
  36.  
  37.    delete aBuffer;
  38.  
  39. Can you tell me why this works? Why doesn't it need the [] after the
  40. delete operator? Is it because char is an internal type?
  41.  
  42.  
  43. My apologies if this is an obvious question - I am still waiting for our
  44. library to get the C++ programming manual, so I do not have a definitive
  45. C++ reference available.
  46.  
  47. Thanks in advance for any help
  48.  
  49. Chris Crook
  50.  
  51. P.S: below is the test program I was working with
  52. =====================================================================
  53.  
  54. // Just testing constructors and destructors on an array
  55.  
  56. #include <iostream.h>
  57.  
  58.  
  59. class MyClass {
  60.    int instance;         // Number of current instance
  61.    static int count;     // counter of instances created
  62. public:
  63.    MyClass();            // Default constructor and destructor
  64.    ~MyClass();
  65.    };
  66.  
  67. int MyClass::count = 0;
  68.  
  69. MyClass::MyClass() {
  70.    instance = ++count;
  71.    cout << "MyClass instance " << instance << " created\n";
  72.    }
  73.  
  74. MyClass::~MyClass() {
  75.    cout << "MyClass instance " << instance << " deleted\n";
  76.    }
  77.  
  78. // Test program includes both automatic and dynamic allocation.
  79.  
  80. int main( int argc, char *argv[] ) {
  81.    MyClass oneArray[5];
  82.    MyClass *anArray = new MyClass[6];
  83.    // delete [] anArray;
  84.    delete anArray;
  85.    return 0;
  86.    }
  87.