[comp.lang.c++] Beginner's Problem Ref:Deallocation of space

jog@umvlsi.ecs.umass.edu (Ashwini ) (03/20/91)

Hi Netters,
  I'm sure this is a beginners problem, but i cant find any 
solution so I hope one of you can help me out.

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 ;
}

I get the the following error :-
malloc/free/realloc: clobbered space detected
Illegal instruction (core dumped)

Is it that if I had said instead "delete pdata", 
the whole array would be destroyed?

Thanks a lot in advance.
ashwini

jog@umvlsi.umass.edu

mittle@blinn.watson.ibm.com (Josh Mittleman) (03/21/91)

> I have created an array
> int *pdata = new int[100] ;

If what you want to do is to free the entire array, then you should use:

delete pdata;

Note that delete DOES NOT set the pointer to 0!!

If you want to free only part of the array, I don't know if it is possible.
Anyone know?


===========================================================================
Josh Mittleman (mittle@ibm.com or joshua@paul.rutgers.edu)
J2-C28 T.J. Watson Research Center, PO Box 704, Yorktown Heights, NY  10598

vaughan@puma.cad.mcc.com (Paul Vaughan) (03/22/91)

	> I have created an array
	> int *pdata = new int[100] ;

	If what you want to do is to free the entire array, then you should use:

	delete pdata;

	Note that delete DOES NOT set the pointer to 0!!

	If you want to free only part of the array, I don't know if it is possible.
	Anyone know?

No, it is not possible to delete only part of the array. In general,
any memory that is allocated as a block must be deallocated as a
block. For instance, you don't want to delete individual members of an
object either.

I believe whether delete sets the pointer to 0 is implementation
dependent, but I don't have the ARM quote handy.
--

 Paul Vaughan, MCC CAD Program | ARPA: vaughan@mcc.com | Phone: [512] 338-3639
 Box 200195, Austin, TX 78720  | UUCP: ...!cs.utexas.edu!milano!cadillac!vaughan
----------------------------------------------------------------------------------
I spent from $3 to $10 today to pay interest on the national debt.  How about you?
----------------------------------------------------------------------------------

jimad@microsoft.UUCP (Jim ADCOCK) (03/26/91)

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."