Allocation of memory for return value in function and memory leaks
I have a function:
char const* GetData() const
{
char* result = new char[32];
sprintf(result, "My super string");
return result;
}
Adn then show this string on the screen like this:
std::cout << GetData() << std::endl;
Or have a class:
class MyClass()
{
char m_data[32]
public:
MyClass(const char* data) { strcpy(m_data, data) } ;
}
And create an instance of an object:
MyClass obj = new MyClass(GetData());
I allocate char* result = new char[32]; and never delete this. How should
I deal with memory leak ? How should I free the memory ?
No comments:
Post a Comment