home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / IDIOMS.ZIP / 3-18.C < prev    next >
C/C++ Source or Header  |  1991-12-04  |  900b  |  34 lines

  1. /* Copyright (c) 1992 by AT&T Bell Laboratories. */
  2. /* Advanced C++ Programming Styles and Idioms */
  3. /* James O. Coplien */
  4. /* All rights reserved. */
  5.  
  6. #include <stddef.h>
  7.  
  8. void *operator new(size_t, void *p) { return p; }
  9.  
  10. struct S {
  11.     S();
  12.     ~S();
  13. };
  14.  
  15. char buf [sizeof(S)][100];
  16. int bufIndex = 0;
  17.  
  18. void f() {
  19.     S a;    // automatic allocation/initialization
  20.  
  21.     S *p = new S;          // explicit allocation
  22.                            // operator new finds the store
  23.                            // automatic initialization
  24.     delete p;              // explicit deletion
  25.                            // automatic cleanup
  26.  
  27.     S *ppp = new (buf) S;  // explicit allocation in buf
  28.                            // automatic initialization
  29.     ppp->S::~S();          // explicit cleanup
  30.  
  31.     // "a" automatically cleaned up/deallocated
  32.     //    on return from f()
  33. }
  34.