Just change from
sizeof
to
strlen()
,
sizeof
is evaluated in compile time.
sizeof
a pointer either gives 4 bytes(32-bit) or 8 bytes(64-bits) and length of your source
text
is longer than 8 bytes.
strlen()
is done during runtime. And set the 1st char of
pmessage
to null character to mean zero length because
pmessage
is point to a new array with random values. Or you can
memset
to zero-initialize the whole array.
strlen()
is checking for null character to determine the string length.
CMessage(const char* text = "default message")
{
if(pmessage) delete [] pmessage; pmessage = new char[strlen(text) + 1];
pmessage[0] = '\0'; cout << " the length of text is:" << strlen(text) << endl;
cout << " the length of pmessage is:" << strlen(pmessage) << endl;
strcpy_s(pmessage,strlen(pmessage)+1,text);
}