i am facing problems of destructor in the following code.destructor is not working
The memory allocation is one byte off, you need to allocate an additional byte for the null terminator placed there by strcpy()
, so
len = strlen(str) + 1;
FYI, sizeof(char)
is always one (1), so you can drop sizeof(char)
altogether.
In the constructor, len is rather 1 than 0, though this does not affect the program as of now.
Then last but not least, it's a good practice to check whether the memory re/allocation succeeds. If they don't, the program crashes.
[EDIT]
Since this is C++, maybe rewrite the program using new/delete
or std::string
.