Results 1 to 6 of 6

Thread: reference to a pointer example and pitfalls

  1. #1
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    68
    Posts
    3,865
    Rep Power
    405

    reference to a pointer example and pitfalls

    In Java you can use the New operator to create an object from Free System Memory and assign it to a Reference. In C++ you have to use New with Pointers.
    I thought well, what if I make a reference to the pointer and then delete it to avoid memory leaks later. I thought this would a be a cool work around, but alas it didn't work.
    PHP Code:
    #include <iostream>

    using namespace std;

    class 
    v3
    {
        private:
            
    int x;
            
    int y;
            
    int z;


        public:
        
    v3(int X 0int Y 0int Z 0)
        {
            
    X;
            
    Y;
            
    Z;
        }


        
    void print( void )
        {
            
    cout << "x: " << << "    y: " << << "    z: " << << "\n";
            
    cout << "\n\n\n\n";
        }
    };


    int main()
    {
        
    cout << " Program memory v3 variable v" << "\n\n";
        
    v3 v362436 );
        
    v.print();


        
    v3vp = new v3442740 );
        
    cout << " Free memory using new operator creating pointer vp" << "\n\n";
        
    vp->print();


        
    v3vr = *vp;
        
    cout << " vr reference to the pointer vp" << "\n\n";
        
    vr.print();


        
    delete vp;
        
    cout << " Deleted pointer vp to avoid memory leaks" << "\n\n\n\n";


        
    cout << " Our reference vr has garbage now" << "\n\n";
        
    vr.print();


        
    cout << " Shame that you can't use the new operator with references in c++" << "\n";
        
    cout << " like you can in Java. I thought perhaps this work around would have worked" << "\n";
        
    cout << " but it didn't." << "\n\n";


        
    cout << " Press Enter to Exit" << "\n\n";

        
    cin.get();

        return 
    0;

    Last edited by kryton9; 20-06-2011 at 03:20.

  2. #2
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    68
    Posts
    3,865
    Rep Power
    405

    another try, still fails

    PHP Code:
    #include <iostream>

    using namespace std;

    class 
    v3
    {
        private:
            
    int x;
            
    int y;
            
    int z;

           
    v3vp;

        public:
        
    v3(int X 0int Y 0int Z 0)
        {
            
    XYZ;
        }

        
    void makevoid )
        {
           
    vp = new v3xy);
        }

        
    v3retvoid )
        {
            
    v3VR = *vp;
            return ( 
    VR );
        }

        
    void destroyvoid )
        {
            
    delete vp;
        }

        
    void print( void )
        {
            
    cout << "x: " << << "    y: " << << "    z: " << << "\n";
            
    cout << "\n\n\n\n";
        }
    };


    int main()
    {
        
    //have one temp of class v3, that can be used to make dynamic new v3's
        //that can be accessed by references

        
    v3 temp(362536);
        
    temp.make();
        
    v3vr temp.ret();
        
    cout << " vr reference before destroy" << "\n\n";
        
    vr.print();

        
    temp.destroy();
        
    cout << " Destroy called to avoid memory leaks" << "\n\n\n\n";

        
    cout << " Our reference vr has garbage still" << "\n\n";
        
    vr.print();

        
    cout << " Press Enter to Exit" << "\n\n";

        
    cin.get();

        return 
    0;

    Last edited by kryton9; 20-06-2011 at 03:19.

  3. #3
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    68
    Posts
    3,865
    Rep Power
    405

    finally, success

    //Make sure to run from the console window//This way you can see the delete message after
    //Program end.
    
    
    //Now I see why so many c++ engines have managers for everything
    //I see why they have this extra layer.
    
    
    //Since I am using MingW, had to add these defines
    //if you are using Visual Studio, won't need these
    #define WINVER 0x0501
    #define _WIN32_WINNT 0x0501
    
    
    #include "windows.h"
    #include <iostream>
    #include <stdlib.h>
    #include <list>
    using namespace std;
    
    
    class v3
    {
    private:
        int x;
        int y;
        int z;
    
    
    public:
        v3(int X = 0, int Y = 0, int Z = 0)
        {
            x = X;
            y = Y;
            z = Z;
        }
    
    
        void print( void )
        {
            //tabbing the output so the columns line up
            cout << "x:" << x << "\t\ty:" << y << "\t\tz:" << z << "\n";
        }
    };
    
    
    // create a vector3 manager to handle dynamic memory for vectors
    class v3mngr
    {
    public:
        //will use a STL list container to store the vector3s
        list<v3 *> v3List;
    
    
        v3mngr() {} //empty constructor
    
    
        // the destructor will free all memory at the end of the program
        ~v3mngr()
        {
            list<v3*>::iterator it; //STL iterator, used to go through items
            // in a container
            for( it= v3List.begin(); it!=v3List.end(); it++ )
            {
                delete ( *it ); // delete the value pointed to in the list via it
            }
            v3List.clear(); //finally free memory of the list pointer
            cout << "auto deleted pointers" << "\n\n";
        }
    
    
        v3& make(v3* v)
        {
            v3List.push_back(v);
            return *v; //returns the value pointed to by v as a reference
        }
    };
    
    
    //Found out about this from Jose's site
    // http://www.jose.it-berater.org/smfforum/index.php?topic=775.0
    long getAvailbleSystemMemory()
    {
        MEMORYSTATUSEX status;
        status.dwLength = sizeof(status);
        GlobalMemoryStatusEx(&status);
        return status.ullAvailPhys;
    }
    
    
    int main()
    {
        long startOfApp, after10Vecs, endOfApp;
    
    
        cout << "startOfApp Available Memory: " << ( startOfApp = getAvailbleSystemMemory() ) << "\n\n";
        //our v3 manager that can be used to make and manage dynamic new v3's
        v3mngr vm;
        int numV3s = 10;
    
    
        //this was the tricky thing to figure out. how to reference an array
        v3 v[numV3s];
        v3 (&vr)[numV3s] = v;
    
    
        for ( int i = 0; i <= numV3s; i++ )
        {
            vr[i] = vm.make( new v3( rand(), rand(), rand() ) );
            vr[i].print();
        }
        cout << "Memory difference after 10 Vectors created: " << startOfApp - ( after10Vecs = getAvailbleSystemMemory() ) << "\n\n";
        cout << "\n\n\n\n";
        cout << " Now when the program ends, the memory should be cleaned automatically" << "\n\n\n\n";
    
    
        cout << " Press Enter to Exit" << "\n\n";
        cin.get();
        cout << "End System Memory: " << ( endOfApp = getAvailbleSystemMemory() ) << "\n\n";
        cout << "End - Start before Pointers Deleted from Memory: " << endOfApp - startOfApp << "\n\n";
        return 0; // program ends
    
    
        // our v3manager destructor gets called here automatically and frees memory
    }
    
    Last edited by kryton9; 20-06-2011 at 04:48.

  4. #4
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    68
    Posts
    3,865
    Rep Power
    405

    [CODE] to [php]

    You might have noticed... I changed the CODE tags to php, it seems to highlight c/c++ code better and also puts the code in a scrolling control.

  5. #5
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,817
    Rep Power
    10
    code tag is dedicated to thinBasic using a javascript script, so executed at client side
    php tag is native to the forum software and executed at server side. php is similar to c in certain cases so its code parsing seems better
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  6. #6
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    68
    Posts
    3,865
    Rep Power
    405
    PHP tag had problems with the last file, so I went back to CODE just for that. Nice to have those two options.

Similar Threads

  1. Irrlicht reference
    By ErosOlmi in forum Irrlicht
    Replies: 1
    Last Post: 05-05-2007, 02:13

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

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