Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!cs.utexas.edu!uunet!microsoft!jimad From: jimad@microsoft.UUCP (Jim ADCOCK) Newsgroups: comp.lang.c++ Subject: Re: Beginner's Problem Ref:Deallocation of space Message-ID: <71514@microsoft.UUCP> Date: 25 Mar 91 20:23:50 GMT References: <1719@umvlsi.ecs.umass.edu> Reply-To: jimad@microsoft.UUCP (Jim ADCOCK) Organization: Microsoft Corp., Redmond WA Lines: 57 In article <1719@umvlsi.ecs.umass.edu> jog@umvlsi.ecs.umass.edu (Ashwini ) writes: |I have created an array |int *pdata = new int[100] ; | |somewhere else i want to free all that space. |If I use the following code | |int *tmp ; |while(pdata){ | tmp = pdata + 1; | delete data ; | data = tmp ; |} According to the ARM, page 64: "The form delete [ ] cast-expression is used to delete arrays. The expression points to an array. The destructors (if any) for the objects pointed to will be invoked. * The user is required to specify when the array is delete...." So, thats the "official" story. Unofficially "C++" was first defined to delete arrays using the form: delete data; then this was changed to the second form: delete [100] data; which was then changed to the third form: delete [] data; Thus, the first problem C++ programmers are going to find is that compilers expecting one or the other of these forms exist. Also, it remains an open issue how, if at all, new and delete work in conjuction with malloc and free. Some Day, presumably, we'll all get the definitive answer on all this from the ANSI-C++ committee. Until then, I can only recommend that you take the time to carefully reverse-engineer how the compilers you work with work with this issue. This is not just a question of how the syntax your compiler is willing to accept. There's also issues of when, if and how destructors are invoked, and the interaction with overloaded operator delete(void*) and/or delete(void*, size_t). The delete syntax problems tie into the C/C++ history of unsafe pointers, where neither the compiler nor the code reader can in general tell whether an int* refers to an int, an int array, an element of an int array, or none-of-the-above. "Sorry."