Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "DELETE" a char * variable return by a function?

Tags:

c++

The function prototype:

char * get_something();

Now we need to free the memory allocated by "get_something" function.

delete or delete []?

we have no information about the impl of the function -- array of char or single char

like image 517
belle tian Avatar asked Dec 18 '22 13:12

belle tian


1 Answers

You can't tell.

And the author of the function should have known better than to return a pointer to allocated memory like this.

Even if you did find out (by consulting the documentation perhaps?), you'd have to make sure that your code was compiled in exactly the same way as that function. If your delete runtime is different from their new runtime then merry hell will ensue.

Your best bet is to badger the library vendor to provide you with a release_something(char*) function that clears the memory for you.

like image 167
Bathsheba Avatar answered Dec 31 '22 14:12

Bathsheba