The Wayback Machine - https://web.archive.org/web/20090214010417/http://www.codeguru.com:80/cpp/cpp/cpp_mfc/oop/article.php/c14963/

CodeGuru
Earthweb Search
Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

follow us on Twitter

Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

jobs.internet.com

internet.commerce
Partners & Affiliates
















Home >> Visual C++ / C++ >> C++ >> C++ & MFC >> Object Oriented Programming (OOP)


Basic Concept of Memory Management in a C++ Class
Rating:

Anpino (view profile)
April 8, 2008

Environment:  Visual Studio 2005, C++, Win32

Go to page: 1  2  3  Next

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.


(continued)



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.

Quiz

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
{
private:
   char* pchTest;
public:
   explicit A(char* const pchInitialText);
   virtual ~A();
   char* GetContent(void)const{return pchTest;}
};

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

A::~A()
{
   delete [] pchTest;
   pchTest = NULL;
}

void show(A clsAtest)
{
   printf("%s",clsAtest.GetContent());
}

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

Hints

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.

About the Author
A R&D; Software Engineer in Taiwan.
International High IQ Society Member, New York.
MCSD(Microsoft Certified Solution Developer (for Microsoft .NET))
Web Site:
Anpino's Circle
English is not my primary language, I will be glad to receive your response, especially the advises that you can provide regarding my poor English.

Go to page: 1  2  3  Next

Tools:
Add www.codeguru.com to your favorites
Add www.codeguru.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed







RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

(You must be signed in to rank an article. Not a member? Click here to register)

Latest Comments:
What is Atest? - KingdomHeart (09/04/2008)
Some comments - DaMagic (04/12/2008)

View All Comments
Add a Comment:
Title:
Comment:
Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



(You must be signed in to comment on an article. Not a member? Click here to register)

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers