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

This is a discussion on What happens to pointer on myFunction(new myClass()); ? within the C++ Programming forums, part of the General Programming Boards category; Did a search on this and didn't come up with anything, and it's starting to baffle me. I was working ...

  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
    21,414
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21