New-ish to C++

HI - I'm newish to C++, but have been a C programmer for many years. I've been asked to look at a small piece of code and briefly explain the most serious problem(s) with the memory management in this class. It's not schoolwork it's the final question of a pre-interview job questionnaire...

1
2
3
4
5
6
7
8
9
class string {
public:
string( char const* t ) : s(t) {}
~string() { delete s; }
char const* c_str() const { return s; }

private:
char const* s;
};


Thanks, in advance, for any help...
"Hey, what's the answer to this question? I could give a shot at answering it, but instead I'll just ask you guys for the answers."

Providing your analysis in addition to the question would go a long way towards nullifying that impression.
I'm guessing the problem is in the destructor.

delete s; will delete the first element in the array but the rest of the array leaks. I think you need delete[] s;.
The problem is not a memory leak, but an attempt to release memory that was never allocated. Where is the new to match the delete?
Topic archived. No new replies allowed.