Iron Attorney
Iron Attorney

Reputation: 1070

Should I delete a pointer if it is a function arguemente and is being passed values by reference?

I'm sure this question has been answered some where, and please link me to it if it has, but I'm struggling to find an answer to this exact question. There are many that are very close. Here are some I've already read that don't quite answer my question:

pass by reference and value with pointers Reason to Pass a Pointer by Reference in C++? Using delete on pointers passed as function arguments

Here is a basic outline of the situation, trying to make it pretty close to my actual usage:

void func (vector<int>* foo) {

    int bar = foo->at(0);

    // transform bar

    foo->at(0) = bar;

    // Should I delete foo?
    delete foo;
}

int main() {

    vector<int> data (4, 0);

    data.at(0) = 5;

    for (int i = 0; i > 10; i++) {

        func(&data);

    }
}

edit: the for loops above should read like this:

for (int i = 0; i < 10; i++) {

So, as you can see, I have created a vector called data, and then passed a reference of data to the function. The function gets a value from data via the reference, transforms it, and then saves the new value back to data again via the reference.

In this specific scenario, this function is the only function that receives this data, but it gets called repeatedly and passed the reference to the same data each time.

My main question is, should I delete foo at the end of that function? Also, if I had an extra function also being passed a reference to data, would that change the answer?

I have read other questions that suggest to be careful deleting a pointer in a function, but they have all related to a data type that was declared as a pointer in the first place. I am not declaring a pointer or reference in the first place, just creating a reference as an argument to the function (I think?).

The reason I ask this anyway, is that my program has some serious memory leaks. I've done some research, and read lots about pointer related memory leaks, and it's occurred to me this might be one source of it

I have tried adding a "delete" at the end of one such function, and all seems well, so far... but I don't want to add a whole bunch of deletes, then some time down the road find out that it was a really bad idea :D

Cheers in advance, Pete

Upvotes: 2

Views: 231

Answers (2)

vladon
vladon

Reputation: 8401

Should I delete foo?

No, you should not.

Moreover, you should never use new or delete. And you shold never pass value by pointer.

Instead, pass it by reference (&), if you want to change it in the function.

Correct code:

void func (vector<int> & foo) {

    try {
        int bar = foo.at(0);
    }
    catch (std::out_of_range) {
        // here you should process an exception 
        // when `foo` contains no elements
    }

    // transform bar

    foo.at(0) = bar;
}

int main() {

    vector<int> data (4, 0);

    data.at(0) = 5;

    for (int i = 0; i > 10; i++) {

        func(data);

    }
}

(Note: may be it must be i < 10 in loop?)

Upvotes: 1

Jarod42
Jarod42

Reputation: 217293

You have to call delete for each new (except placement new which is special).

Here, no new, so no delete.

Upvotes: 5

Related Questions