home *** CD-ROM | disk | FTP | other *** search
/ PC World Interactive 7 / PC World Interactive 7.iso / program / cprog.EXE / DELTEST.ZIP / DELTEST.CPP next >
Encoding:
C/C++ Source or Header  |  1991-11-29  |  2.5 KB  |  83 lines

  1. // Stress-Test C++ 'new' and 'delete'.  Written in Borland/Turbo C++.
  2. // Written by George Lerner, Lerner Computer Consulting, 1991. (415) 586-6233.
  3. // You may copy this program freely.
  4.  
  5. // Environment: Northgate 386/16, Hercules monitor, QEMM-386 v6.0, MS-DOS 5
  6. // Tested under DesqView 2.31, QEMM, and just MS-DOS 5.
  7.  
  8. // Requires C++.  Will not compile under C.
  9. // Borland C++: \bcpp\bin\bcc +glturboc.cfg -c -mm deltest.cpp
  10.  
  11. // Easiest way to avoid this might be to overload new and delete for your
  12. // object, so when delete is called, the object's address is assigned NULL,
  13. // and global delete is not called unless the object != NULL.
  14.  
  15. /* Example of this bug in an actual program I wrote:
  16. class EntryField : public Object {
  17.     friend EntryForm;
  18. ...etc...
  19. };
  20. class EntryForm : public Wndo, Array {    // my windowing class, BC++ Array
  21. ...etc...
  22. };
  23. // Add an EntryField to the EntryForm
  24. void EntryForm::add(EntryField &field) {
  25.     ...other stuff...
  26.     Array::add(field);    // call the base class's add to do the rest of the work
  27. }
  28.  
  29. // destructor
  30. EntryForm::~EntryForm(void) {
  31.     for (int i=0; i < num_fields; i++) {
  32.         detach(i);    // AbstractArray::detach(int)
  33.         // ABSOLUTELY REQUIRED to detach() the fields.    Otherwise, the
  34.         // fields will be deleted BOTH in ~EntryField() and in
  35.         // ~AbstractArray().  Took me ages to trace the program hanging upon
  36.         // executing the } of main().
  37.     }
  38.     // automatically calls base class destructors
  39.     // ~Array();
  40.     // ~Wndo();
  41. }
  42. */
  43.  
  44. #include <stdlib.h>
  45. #include <iostream.h>
  46.  
  47. void main() {
  48.     char *s = new char[256];
  49.     char *t;
  50.     char c;
  51.  
  52.     cout << "This program stresses 'new' and 'delete'.\n";
  53.     cout << "Shows can NOT delete something multiple times without problems!\n";
  54.     cout <<
  55.     "\n\n   T H I S   P R O G R A M   C A N   H A N G   C O M P U T E R S ! \n";
  56.  
  57.     cout << "\n\nProgram starts by declaring strings s, and (unassigned) t.";
  58.     cout << hex << "\ns=" << (long)s <<  "\n";
  59.  
  60.     int i;
  61.     for (i=1; i <= 10; i++) {
  62.         cout << "Count: " << i << " of 10\n";
  63.         cout << "Delete S, new T\n";
  64.         cout << "   (T should have same address as S)\n";
  65.         delete s;
  66.         t = new char[256];
  67.  
  68.         cout << "S=" << (long) s << "  T=" << (long) t << '\n';
  69.  
  70.         cout << "delete T, new T\n";
  71.         cout << "   (T should have same address as S)\n";
  72.         delete t;
  73.         t = new char[256];
  74.         cout << "S=" << (long) s << "  T=" << (long) t << '\n';
  75.  
  76.         cout << "delete T\n";
  77.         delete t;
  78.  
  79.         cout << "--- Press Return (or Ctrl-C to Quit) ---\n";
  80.         cin.get(c);     // get a character from keyboard
  81.     }
  82. }
  83.