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