Pointer question

Hey!

I've a question when it comes to pointers and allocation.

Lets say we've a class called MyVector.

class MyVector
{
private :
float *arr;
public :
MyVector()
{
arr = new float[20];
}
float* getVector()
{
return arr;
}

};

The question:
When i return it from my vector class, do i have to delete it both in the class and in main? Or, if i delete it in main, i deleted the same pointer?

Whats the difference between:

float* getVector()
{
return arr;
}

and

float*& getVector()
{
return arr;
}

pointers are not automatically call by-reference right?

Thanx for answers. Pointers sometimes screw with my brain.
This is what a class destructor is for. Simple example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class HoldInt
{
    int *p;
public:
    HoldInt() : p(new int()) {}
    HoldInt(const HoldInt &from) : p(new int(*from.p)) {}
    HoldInt(int i) : p(new int(i)) {}
    HoldInt &operator=(const HoldInt &from)
    {
        *p = *from.p;
        return*this;
    }

    void Value(int i)
    {
        *p = i;
    }
    int Value() const
    {
        return *p;
    }

    ~HoldInt() //This is the destructor, same as default constructor but with tilde ~
    {
        delete p; //this is not done for you because you allocated the memory with new
    }
};


This way you never have to worry about manually deleting the class' internal stuff - it's all handled for you at this point.
Last edited on
Thanx for the reply, but im not noob in c++. I'm just confused.

The question is: if a return a pointer from a class and store it in a variable in main, do i have to delete the pointer in main and in the class to prevent memory leak? Or, if i delete the pointer in main, do i destroy the pointer data in the class?

Sry if i wasn't clear with my question.
Hi

If you return a pointer from your class, you should not delete it outside of your class, the destructor does that. However, it may be safer if you use smart pointer in such cases, instead of your class.

But if you have a class that just return a new create pointer without assigning it to any internal members, than you have to delete that pointer out side of your class.

a simple example for that case

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class myVec{

      public:

       int* operator()(unsigned int size){
                return (new int[size] );
       }

};
int main() {

	int* p = myVec()(10);

	for(int i = 0 ; i < 10 ; i++)
		p[i] = (i + 1) *2;

	for(int i = 0 ; i < 10 ; i++)
		cout<<p[i]<<endl;
	
        delete []p;

	return 0;
}


hope it helps
Last edited on
HOWEVER, if you do something like this, you should delete it in the calling code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Something
{
    //...
public:
    //...
    Thing *getStatus()
    {
        if(Evil)
        {
            return new Thing(Stuff);
        }
        else return 0;
    }
    //...
};
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
    Something s;
    //...
    Thing *t = s.getStatus();
    if(t)
    {
        //...
    }
    delete t; //it is your responsibility to delete it
    //because how could the class possibly know when you were done with it?
    //...
}
Last edited on
@L B

I think I said ( or posted the same things/concepts) , didn't I ? :- )
Last edited on
Oops. I misread.

>_<

Well, three examples are better than two.
Thanx all. I think I understand.
Topic archived. No new replies allowed.