Consider the following two cases:
CCandyBox obj;
str will be pointing to "Candy"
May I know "Candy" is on heap or stack?
If it is on heap, is the
delete[] str; //delete the Candy to avoid memory leak
line below necessary?
CCandyBox(char* str = "Candy") // Constructor
{
if (str == NULL)
{
m_Contents = new char[1]; // will be an empty string
*m_Contents = 0; //terminate string
}
else
{
m_Contents = new char[ strlen(str) + 1 ];
strcpy_s(m_Contents, strlen(str) + 1, str);
delete[] str; //delete the Candy to avoid memory leak
}
}