home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16526 < prev    next >
Encoding:
Internet Message Format  |  1992-11-18  |  2.3 KB

  1. Path: sparky!uunet!sun-barr!cs.utexas.edu!sdd.hp.com!wupost!waikato.ac.nz!comp.vuw.ac.nz!newshost!srlncnc
  2. Newsgroups: comp.lang.c++
  3. Subject: Re: delete with arrays - help please
  4. Message-ID: <1992Nov18.181425.6817@gopher.dosli.govt.nz>
  5. From: srlncnc@gopher.dosli.govt.nz (Chris Crook)
  6. Date: Wed, 18 Nov 1992 18:14:25 GMT
  7. Sender: srlncnc@gopher.dosli.govt.nz (Chris Crook)
  8. Organization: Department of Survey and Land Information, New Zealand
  9. Lines: 41
  10.  
  11.  
  12. If responding e-mail please use this address:
  13.  
  14.        From: carroll@sde.mdso.vf.ge.com (Carroll James)
  15.  
  16. The reason for this is simple. new and delete really use 'malloc' and 'free' 
  17. (or similar systems calls to the heap manager) to allocate a bocks of memory.
  18.  
  19. The heap manager knows how big of a block has been allocated but it DOES NOT
  20. know anything about data types in those blocks. Because of this the call of the
  21. destructors is done in C++ and not by the 'system' (in this case, the heap 
  22. manager, which is part of the system).
  23.  
  24. In older versions of C++ (2.0 ?) you couldnt use a [] without a number in it 
  25. because the delete operator (which is really a library routine in C++) didnt
  26. get a count from the heap manager. The entire reason for the [n] was for the
  27. delete operation to call the destructor 'n' times. In newer versions of C++ 
  28. the delete operator became smart enough to know (or to figure out based on size
  29. of element being deleted and the size of the block allocated) what 'n' is and
  30. call the destructor that many times.
  31.  
  32. In either case (i.e. delete x, or delete [n] x) a call is made to 'free' 
  33. (which would look like free(x)) at which point the entire block is freed by
  34. the heap manager. The problem is that for Object instancted with destructors,
  35. the destructors will not all be called unless the [n] is supplied (remember
  36. that delete without [] or [n] assumes there is only one).
  37.  
  38. For built in types or object instances that dont have destructors, the total block
  39. of memory is freed. Therefore for these instances or built in types the overall
  40. effect is the same for both 'delete x' and 'delete [] x'.
  41.  
  42. I think ARM mentions some precautions to take. I personally dont like making a 
  43. distinction between built in types and object instances when it can be avoided
  44. but in this case it will work.
  45.  
  46.  
  47.                 Jim Carroll
  48.  
  49. PS Could you do me a favor and Post this to the newsgroup since that capability
  50. has been disabled on our server.
  51.  
  52.