New to C++, pointer problem
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: New to C++, pointer problem

  1. #1
    Join Date
    Dec 2008
    Posts
    2

    New to C++, pointer problem

    Hi everyone,

    I'm heading into my second CS course this semester. It's going to cover OOP using C++. Now I know OOP rather well through teaching myself Obj-C and Cocoa, but I'm trying to get myself familiarized with C++ before the start of the semester. I decided to build an app using Objective-C++, incorporating a Cocoa/Obj-C view and C++ model. I started to get some odd output in the console window. I then created a stripped down C++ console version of the app and had the same problems. If only happens when I have an object return the pointer of another object.
    Code:
    int main (int argc, char * const argv[]) {
        
    	ObjectOne object1;
    	ObjectOne *obj1Ptr = &object1;
    	
    	// Console Output
            // 0
            // -1605028608
            cout << obj1Ptr->obj2Ptr->theCount << endl;
    	cout << obj1Ptr->obj2Ptr->theCount << endl;
    	
            // Console Output
            // 0
            // 0
    	cout << obj1Ptr->theCounter << endl;
    	cout << obj1Ptr->theCounter << endl;
    	
        return 0;
    }
    
    ObjectOne.h
    #include <stdio.h>
    #include "ObjectTwo.h"
    using namespace std;
    
    class ObjectOne {
    public:
    	
    	ObjectTwo *obj2Ptr;
    	
    	int theCounter;
    	
    	// FUNCTIONS
    	ObjectTwo* getObj2Ptr();
    	int getTheCounter();
    	
    	ObjectOne();
    };
    
    ObjectOne.cpp
    #include "ObjectOne.h"
    
    ObjectTwo* ObjectOne::getObj2Ptr()
    {
    	return obj2Ptr;
    }
    
    int ObjectOne::getTheCounter()
    {
    	return theCounter;
    }
    
    ObjectOne::ObjectOne()
    {
    	ObjectTwo object2;
    	obj2Ptr = &object2;
    	
    	theCounter = 0;
    }
    
    ObjectTwo.h
    #include <stdio.h>
    using namespace std;
    
    class ObjectTwo {
    public:
    	
    	int theCount;
    	
    	int getTheCount();
    	
    	ObjectTwo();
    };
    
    ObjectTwo.cpp
    #include "ObjectTwo.h"
    
    int ObjectTwo::getTheCount()
    {
    	return theCount;
    }
    
    ObjectTwo::ObjectTwo()
    {
    	theCount = 0;
    }
    Thanks

    Doug
    Last edited by Andreas Masur; January 7th, 2009 at 05:11 PM. Reason: Added code tags...

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: New to C++, pointer problem

    Using code tags would make that easier to read. However, here's your problem:

    Code:
    ObjectOne::ObjectOne()
    {
        ObjectTwo object2;
        obj2Ptr = &object2;
    
        theCounter = 0;
    }
    When the function is called, object2 is created on the stack. You then keep a pointer to it. However, when the function returns, the stack unwinds and object2 is destroyed. At that point the pointer is directed at garbage.

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,391

    Re: New to C++, pointer problem

    Please post your well indented code in [code][/code] bbcode tags.

    At a glance, one possible problem is that you are assigning the address of a local variable to a member variable in the ObjectOne constructor. This local variable is destroyed at the end of the function, upon which the member pointer points to a destroyed object. Do you even need a pointer in the first place?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: New to C++, pointer problem

    There are several possible solutions to this issue. I'll attempt to lay out the pros and cons of a few.

    1) Make object2 a class member of ObjectOne to begin with, rather than making the pointer a member. This is fine for the most part, and probably the easiest solution to the issue. The downside is that it requires ObjectTwo.h to be #included in ObjectOne.h----you can't use a forward-declare. However, here you're doing that anyway.

    2) Allocate object2 on the heap using new and assign it to the pointer. The downside here is that you are required to write a destructor now to free that memory, and also a copy constructor and assignment operator (or disable those operations by making them private). Classes written this way tend to get unwieldy very fast, I don't recommend it.

    3) Do the same as (2), but use a std::tr1::shared_ptr<ObjectTwo> rather than an ObjectTwo*. Then all copying and destruction will be handled automatically. This option should be preferred to (1) only if there's some reason you need the class member to be a pointer----say, for polymorphism or to enable a forward-declaration to break an include cycle.

    The lesson here: Don't overuse pointers. And when you do use them, consider using a smart pointer rather than a "bare" pointer. It will make your life easier.

  5. #5
    Join Date
    Dec 2008
    Posts
    2

    Re: New to C++, pointer problem

    Thanks everyone, that helped a lot.

    Doug

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center