Q
qazmlp
Is there a memory leak here? Can't the memory deallocation happen
automatically as the std::string object anyway is owning the memory?
#include <cstring>
#include <string>
#include <iostream>
char* toString()
{
const char* cStr = "cStr" ;
char* ptr = new char[ std::strlen( cStr ) + 1 ] ;
std::strcpy( ptr , cStr ) ;
return ptr ;
}
int main()
{
std::string cppStr( toString() ) ;
std::cout<< cppStr << std::endl;
}
automatically as the std::string object anyway is owning the memory?
#include <cstring>
#include <string>
#include <iostream>
char* toString()
{
const char* cStr = "cStr" ;
char* ptr = new char[ std::strlen( cStr ) + 1 ] ;
std::strcpy( ptr , cStr ) ;
return ptr ;
}
int main()
{
std::string cppStr( toString() ) ;
std::cout<< cppStr << std::endl;
}