Click here to Skip to main content
15,883,623 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

Let's say I have the following code:
consoleHelper.write_status("Error: " + (char*)gai_strerror(err_code));

There's a cast there to a char* and there's no delete called on it. Is this memory somehow automatically managed, or would you have to do something like the following?
char* cast_ch = (char*)gai_strerror(err_code);
consoleHelper.write_status("Error: " + cast_ch);

delete cast_ch;

Even if the memory used is tiny, I'd like to prevent all memory leaks wherever possible to ensure the application can theoretically run forever with no issues.

What I have tried:

Nothing, just asking for advice
Updated 6-Jul-18 3:27am
Richard MacCutchan 6-Jul-18 9:23am    
No you cannot delete it unless the documentation for that function explicitly tells you to.

You should NOT release memory (it is not explicitely required in the documentation and, moreover, you don't know how the library actually allocates it).

Please note: if there is a leak, that's a (unlikely) library's author mistake (and probably harmless). On the other hand, calling delete on the pointer would be dangerous and entirely of your responsibility. :-)
Share this answer
[no name] 6-Jul-18 8:21am    
Which library are you talking about? Which documentation? The code behind the cast cannot tell when it's finished being used, so it can't possibly free the memory itself, surely?
CPallini 6-Jul-18 8:35am    
The library implementing the gai_strerror function. The documentation of such a library. If you don't know how memory is allocated (and what C runtime did allocate it) the you must NOT release it.
[no name] 6-Jul-18 8:43am    
All of the code apart from gai_strerror is my own.
CPallini 6-Jul-18 9:50am    
The string comes just from gai_strerror.
Let's face it: trying to release memory on such a pointer is a plain mistake.
[no name] 6-Jul-18 10:58am    
But I want to understand the reason *why*.
The documentation (which you should always check first) : gai_strerror[^] tells you that the return is a pointer to a character constant, so you should not do anything with it.
Share this answer
[no name] 6-Jul-18 10:54am    
Why exactly does it being a const char* mean you shouldn't do anything with it? I'm trying to understand the reasons why these things are what they are.
Richard MacCutchan 6-Jul-18 11:47am    
Because the thing being pointed at is a constant, that is to say, it cannot be modified. And you cannot create a constant by using new or malloc, since dynamically created memory space can always be modified.
[no name] 6-Jul-18 11:50am    
Ok so const pointers never need to be deleted in any scenario?
Richard MacCutchan 6-Jul-18 11:54am    
That's a good rule of thumb. Although the use of const can sometimes cause confusion, see The C++ 'const' Declaration: Why & How[^].
Richard MacCutchan 6-Jul-18 11:58am    
And another issue to be careful of. You should not cast that pointer to a simple char*, as you are casting away its const'ness, thus leaving it open to misuse.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900