As others already pointed out, technically, the code is okay.
However, the real problem with the code is the function signature char * func()
.
It only specifies that it will return a pointer to a char. That pointer can be
- a normal pointer to a char variable
- a pointer to a char array
- a pointer to a c-style string
The pointer can point to
- static memory
- heap-allocated memory that will be freed somewhere else
- heap-allocated memory that you are suppose to free yourself.
There is no way of knowing what to do unless you want to trust the documentation or to investigate in possibly a lot of code (functions calling functions calling functions...).
This can be avoided by returning
- a char
- an std::string
- a container
- a smart pointer.
Manual memory management is hard to get right within a complete application and should never be your main concern, so it should be avoided and not hidden by functions returning pointers.