[comp.lang.c++] Is it okay to use delete after malloc?

ahodgson@athena.mit.edu (Antony Hodgson) (01/29/91)

If I create a string using one of the library routines such as strdup,
is it then acceptable to use delete, or is free() required?

E.g.:

	char* s;
	s = strdup( "Test" );
	delete s;   // or free(s)?

Tony Hodgson
ahodgson@hstbme.mit.edu

steve@taumet.com (Stephen Clamage) (01/31/91)

ahodgson@athena.mit.edu (Antony Hodgson) writes:

>If I create a string using one of the library routines such as strdup,
>is it then acceptable to use delete, or is free() required?

Using 'delete' on a pointer is not guaranteed to work if the pointer
was not obtained from  'new'.  If space is acquired by malloc(), you
should return it with free().
-- 

Steve Clamage, TauMetric Corp, steve@taumet.com

jimad@microsoft.UUCP (Jim ADCOCK) (02/07/91)

In article <574@taumet.com> steve@taumet.com (Stephen Clamage) writes:
|ahodgson@athena.mit.edu (Antony Hodgson) writes:
|
|>If I create a string using one of the library routines such as strdup,
|>is it then acceptable to use delete, or is free() required?
|
|Using 'delete' on a pointer is not guaranteed to work if the pointer
|was not obtained from  'new'.  If space is acquired by malloc(), you
|should return it with free().

I agree today.  The ANSI-C++ committee is looking into what interactions
should be allowed/disallowed in terms of new vs malloc, and stream i/o vs
stdio, so things could become easier on programmers in the future.

Today, things are implementation defined, and you're more likely of success 
if you match your malloc/free's, new/delete's, and new[]/delete[]'s.