Thread: What happens to pointer on myFunction(new myClass()); ?

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    416

    What happens to pointer on myFunction(new myClass()); ?

    Did a search on this and didn't come up with anything, and it's starting to baffle me. I was working on some class functions that require a pointer to another class. I was originally implementing as below, but then changed it to the second chunk of code. When I changed it to the second I noticed my values were now gone, and my objects no longer existed.

    So when you use a function in this manner, myFunction(new myClass()); does the pointer to the newly created myClass get deleted after the function returns?

    Code:
    // this is the way i implemented it, and stored values,
    // using assign(new someOtherClass()); works fine in this case
    // and 'data' retains the information, but what happens to
    // the pointer to the newly made someOtherClass? Memory leak???
    
    class myClass
    {
    private:
        someOtherClass data;
    
    public:
        
        // ignore that there are no constructors
        
        void assign(someOtherClass* otherClass)
        {
            data = (*otherClass);
        }
    };
    Code:
    // this is the second way I implemented it, thinking it was better,
    // but 'data' ends up not retaining the pointer after the
    // assign(new someOtherClass()) function is called, what gives???
    
    class myClass
    {
    private:
        someOtherClass* data;
    
    public:
        
        // ignore that there are no constructors
        
        void assign(someOtherClass* otherClass)
        {
            if (data != NULL) delete data;
            data = otherClass;
        }
    };

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by scwizzo
    So when you use a function in this manner, myFunction(new myClass()); does the pointer to the newly created myClass get deleted after the function returns?
    No, it does not get deleted.

    Your first version does result in a memory leak. Your second version should be closer to being correct, so I suggest that you post the smallest and simplest compilable program that demonstrates the problem. The question to ask is: does your myClass object have ownership of the someOtherClass object that its member variable named data points to?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Code:
    // this is the second way I implemented it, thinking it was better,
    // but 'data' ends up not retaining the pointer after the
    // assign(new someOtherClass()) function is called, what gives???
    Hmm, well did you delete otherClass before you checked data? Because data and otherClass share memory now. Once either of those pointers are released by delete, you could dereference either of them, and ask "what gives?" in response. You could use reference counting if you want things to remain mostly the same....

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Quote Originally Posted by laserlight
    so I suggest that you post the smallest and simplest compilable program that demonstrates the problem
    *Slams head on desk* When I went back to find the code for copy-pasting, I ended up noticing I was deleting the argument passed to the function (before it was used), instead of the class' member variable that I was originally wanting. Sometimes I surprise myself...

    Quote Originally Posted by whiteflags
    Hmm, well did you delete otherClass before you checked data?
    Yes that sums up what I was doing.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ever thought of using smart pointers?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to a function pointer
    By @nthony in forum C Programming
    Replies: 3
    Last Post: 05-30-2010, 05:13 PM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM