PDA

View Full Version : reference to a pointer example and pitfalls



kryton9
19-06-2011, 14:36
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.


#include <iostream>

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 )
{
cout << "x: " << x << " y: " << y << " z: " << z << "\n";
cout << "\n\n\n\n";
}
};


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


v3* vp = new v3( 44, 27, 40 );
cout << " Free memory using new operator creating pointer vp" << "\n\n";
vp->print();


v3& vr = *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;
}

kryton9
19-06-2011, 15:29
#include <iostream>

using namespace std;

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

v3* vp;

public:
v3(int X = 0, int Y = 0, int Z = 0)
{
x = X; y = Y; z = Z;
}

void make( void )
{
vp = new v3( x, y, x );
}

v3& ret( void )
{
v3& VR = *vp;
return ( VR );
}

void destroy( void )
{
delete vp;
}

void print( void )
{
cout << "x: " << x << " y: " << y << " z: " << 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(36, 25, 36);
temp.make();
v3& vr = 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;
}

kryton9
20-06-2011, 03:09
//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
}

kryton9
20-06-2011, 03:22
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.

ErosOlmi
20-06-2011, 08:08
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

kryton9
20-06-2011, 08:32
PHP tag had problems with the last file, so I went back to CODE just for that. Nice to have those two options.