Is the C-Style malloc and free really necessary here? I think new and delete would be more appropriate. 
But why is there dynamic allocation at all?
Wouldn't it be possible to use a `std::string` right away and use `c_str()` where needed?