Solved

Pointer Problem

Posted on 2006-06-09
6
242 Views
Last Modified: 2013-11-20
Could anyone tell me why the following code does not work?


#include "stdafx.h"

class TestClass : public CObject
{
private:
      CString fname;
      CString lname;

public:
      TestClass(LPCTSTR first, LPCTSTR last);
      CString getFirstName();
      CString getLastName();
};


TestClass::TestClass(LPCTSTR first, LPCTSTR last)
{
fname=first;
lname=last;
}

CString TestClass::getFirstName()
{
return fname;
}

CString TestClass::getLastName()
{
return lname;
}

TestClass* createObject()
{
TestClass t("ABC", "DEF");

return &t;
}



int _tmain(int argc, _TCHAR* argv[])
{
TestClass *tth=NULL;
tth=createObject();//Gives Error
CString test_cstr=tth->getLastName();      

      return 0;
}

0
Comment
Question by:csound
  • 3
6 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 50 total points
ID: 16868702
TestClass* createObject()
{
TestClass t("ABC", "DEF");

return &t;
}

t goes out of scope and is destroyed.


TestClass* createObject()
{
TestClass* t = new TestClass("ABC", "DEF");

return t;
}

don't forget you need to use delete on the returned pointer when no longer required.
0
 
LVL 7

Assisted Solution

by:nafis_devlpr
nafis_devlpr earned 20 total points
ID: 17080219
I agree with Andy. But you can use auto_ptr rather than using new and delete. auto_ptr will be automatically deleted when it is not in use anymore. For using auto_ptr you have to add the two lines at the top of your source file

#include <memory>  
using std::auto_ptr; // auto_ptr class definition

TestClass* createObject()
{
     auto_ptr< TestClass > t( new TestClass("ABC", "DEF") );
     return t;
}

Nafis
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 17080433
To be honest I am not a fan of the garbage collection or mechanisms like that.
If I have a memory leak then it means I have made a mistake in the logic/coding.  Anything that automatically cleans up will hide coding mistakes like that from the programmer.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 17294135
My comment pointer out what was going wrong and how to correct it.
0
 

Author Comment

by:csound
ID: 17295371
Thank you for your help!=)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now