Basic Concept of Memory Management in a C++ Class

Have you ever paid attention to memory management when using classes? I have an elementary quiz about the basic concept of memory management in standard C++. This quiz is from my teaching experience because I found many programmers usually make the same mistake.

Now, take a look at the quiz. Please don’t worry if you cannot give a answer. Paying attention to such code from now on is the most important thing you need to learn.


How does the following code work?

What will happen during the running process? Why?

If there is a problem, how will you solve it?

class A
   char* pchTest;
   explicit A(char* const pchInitialText);
   virtual ~A();
   char* GetContent(void)const{return pchTest;}

A::A(char* const pchInitialText)
   pchTest = new char[strlen(pchInitialText)+1];

   delete [] pchTest;
   pchTest = NULL;

void show(A clsAtest)

int main(void)
   A Atest("Just Test!");
   return 0;


How is the parameter transferred into the functions as shown below?

void FunctionName(ValueType  para);
void FunctionName(ValueType *para);
void FunctionName(ValueType &para);

Have any idea? I will share my thoughts on the next page.

More by Author

Must Read