How can i release the memory?

I write a program like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class OBJ
{
public:
	OBJ(int n):a(n){cout<<"ctor"<<'\n'; }
	~OBJ(){ cout<<"dctor"<<'\n';}
	int geta(){ return a;}
protected:
private:
	int a;
};

OBJ * getobj( )
{
	OBJ *p=new OBJ(10);
	return p;
}

note the function getobj, in this fuction, new allocates memory that is pointed by p, but in this function, p can't be deleted. That may cause memory leak? How to avoid this?
You can free the memory outside the function,
Here is an example:
1
2
3
OBJ *ptr = getobj();
//do something
delete ptr;// memory released 
Last edited on
Topic archived. No new replies allowed.