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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!wupost!uwm.edu!linac!att!cbnewsk!pegasus!hansen
  3. From: hansen@pegasus.att.com (Tony L. Hansen)
  4. Subject: Re: delete with arrays - help please
  5. Organization: AT&T
  6. Date: Thu, 19 Nov 1992 16:48:00 GMT
  7. Message-ID: <1992Nov19.164800.707@cbnewsk.cb.att.com>
  8. Summary: always use delete[] with arrays of all types
  9. Keywords: c++, destructors, delete[], arrays
  10. References: <1992Nov18.181425.6817@gopher.dosli.govt.nz>
  11. Sender: hansen@cbnewsk.cb.att.com (tony.l.hansen)
  12. Lines: 69
  13.  
  14. < From: carroll@sde.mdso.vf.ge.com (Carroll James)
  15. < (via From: srlncnc@gopher.dosli.govt.nz (Chris Crook))
  16. <
  17. < The reason for this is simple. new and delete really use 'malloc' and
  18. < 'free' (or similar systems calls to the heap manager) to allocate a bocks
  19. < of memory.
  20. <
  21. < The heap manager knows how big of a block has been allocated but it DOES
  22. < NOT know anything about data types in those blocks. Because of this the
  23. < call of the destructors is done in C++ and not by the 'system' (in this
  24. < case, the heap manager, which is part of the system).
  25. <
  26. < In older versions of C++ (2.0 ?) you couldnt use a [] without a number in
  27. < it because the delete operator (which is really a library routine in C++)
  28. < didnt get a count from the heap manager. The entire reason for the [n] was
  29. < for the delete operation to call the destructor 'n' times. In newer
  30. < versions of C++ the delete operator became smart enough to know (or to
  31. < figure out based on size of element being deleted and the size of the
  32. < block allocated) what 'n' is and call the destructor that many times.
  33. <
  34. < In either case (i.e. delete x, or delete [n] x) a call is made to 'free'
  35. < (which would look like free(x)) at which point the entire block is freed
  36. < by the heap manager. The problem is that for Object instancted with
  37. < destructors, the destructors will not all be called unless the [n] is
  38. < supplied (remember that delete without [] or [n] assumes there is only
  39. < one).
  40. <
  41. < For built in types or object instances that dont have destructors, the
  42. < total block of memory is freed. Therefore for these instances or built in
  43. < types the overall effect is the same for both 'delete x' and 'delete []
  44. < x'.
  45.  
  46. There are other implementations of which you are evidently not aware for
  47. which this won't work.
  48.  
  49. Consider this common implementation:
  50.  
  51.     Invoking "X *px = new X[10]" will invoke "operator new(sizeof(size_t) +
  52.     sizeof(X)*10)". Into that returned address, the size of the array (10 in
  53.     this case) will be placed into the first four (actually, sizeof(size_t))
  54.     bytes. The function which handles "new X[10]" will then add four bytes
  55.     to the address, invoke the 10 constructors, and then return that new
  56.     address. That is, the address stored into px, as returned by "new
  57.     X[10]", will be four bytes beyond that returned by the "operator new()".
  58.  
  59.     When "delete[] px" is invoked, delete[] will subtract four bytes from
  60.     the address it's passed, retrieve the count, invoke the destructor on
  61.     those objects, and pass the new address (px-4) to "operator delete()".
  62.  
  63.     (Most current implementations which use this scheme only do it if the
  64.     class has a destructor, and don't store the count otherwise. But such an
  65.     optimization is not required.)
  66.  
  67. What happens if you invoke "delete px"? The value passed to "operator
  68. delete()" will NOT be an address that was returned from "operator new()"!
  69.  
  70. Can you say "core dump"? I knew you could.
  71.  
  72. < I think ARM mentions some precautions to take. I personally dont like
  73. < making a distinction between built in types and object instances when it
  74. < can be avoided but in this case it will work.
  75.  
  76. This is all implementation dependent, and on some implementations you may be
  77. able to get away with just invoking "delete px". But don't count on it!
  78.  
  79.                     Tony Hansen
  80.                 hansen@pegasus.att.com, tony@attmail.com
  81.                 att!pegasus!hansen, attmail!tony
  82.  
  83.