Should delete be done after failed new?

1
2
3
data_ = new(nothrow) string( data );
if ( !data_ )
    goto error;


Should I add "delete data_;" or it would end even worse?
(Thanks for replies, some of you here are very responsive, thank you!)
The normal way to assign memory to a pointer (using nothrow):
1
2
3
4
5
6
int *p = NULL; //declare pointer
p = new (nothrow) int [SIZE]; //assing memory
if (p == NULL) //check or assigment failed
{
    return false; //handle the error
}


There is no need to free the memory, since there is no memory reserved in the first place.

Try to avoid confusing names like "data_" and "data", because this will make your code harder to understand for others/yourself when you come back to it. And don't use goto's.
Topic archived. No new replies allowed.