Copying a vector into a vector member of a class

I have a class with a vector member. I have accessors to set/get the value of the vector.

typedef vector<int> vint_t;

class MyVector {
  public:
    MyVector(const vint_t & v) : _vint(v) {};

    size_t set(const vint_t & v) {
        _vint = v;  // Will this leak memory in the old location of _vint?
        const size_t n = v.size();
        return n;
    }

    const vint_t & get() {
        return _vint;
    }

  private:
    vint_t _vint;
};

Besides the fact that set correctly sets the contents of _vint (verified with code below, operator<< suitably overloaded), would set leak the memory at the old location of _vint?

vint_t v1 = { 1, 4, 10, 2 };
MyVector mv1(v1);
cout << mv1.get() << endl;
vint_t v3 = { 8, 1, 4, 10, 2 };
mv1.set(v3);
cout << mv1.get() << endl;

Upvotes: 0

Views: 218

Answers (6)

Paul Floyd
Paul Floyd

Reputation: 6906

This is not an answer to the question, but I do see two dangers in this code.

First

size_t set(const vint_t & v) {
    _vint = v;  // Will this leak memory in the old location of _vint?
}

The function returns size_t but has no return statement. I would expect compilers to complain about this. The function should probably return void.

const vint_t & get() {
    return _vint;
}

Use this with caution. You need to be careful that the lifetime of the MyVector object is not shorter than any reference returned by get. For instance, a short contrived example:

MyVector* mvp = new MyVector(v1);
const vint_t& vref = mvp->get();
delete mvp;
// ...
std::cout << vref << '\n'; // oh dear

Upvotes: 0

eerorika
eerorika

Reputation: 238311

would set leak the memory at the old location of _vint?

No. The memory location of _vint didn't change in the first place. Furthermore, the assignment operator of std::vector is not specified to leak memory (nor is any operation of any standard container).

Of course, if the objects stored in the vector were handles to some resource, then those would leak if the handle is removed without releasing the resources. In your example you've used arbitrary integer values, so they appear to not be resource handles.

Upvotes: 1

463035818_is_not_an_ai
463035818_is_not_an_ai

Reputation: 122228

size_t set(const vint_t & v) {
    _vint = v;  // Will this leak memory in the old location of _vint?
}

There is no way this can leak memory unless std::vector would be terribly broken. In general after a = b; the object a is still the same object. Only the value gets copied. You are calling std::vector::operator= which isn't broken.

Upvotes: 1

Miguel
Miguel

Reputation: 2219

_vint = v

This is a copy assign operation, so the content of v are copied to _vint, but you don't delete or create any object, so there is no possible leak of memory. Also, remember that leaks of memory mostly happen when you are working with pointers, and you are not.

Upvotes: 0

Maurycyt
Maurycyt

Reputation: 718

Since _vint is an object, it doesn't "change location". Pointers don't change location either, but can cause memory leaks if you allocate extra memory via malloc or new.

In your example though, you are neither using pointers nor allocating new memory. If you aren't allocating new memory, there will be no memory leaks, you will be using only as much memory as necessary and all of it will be accounted for when exiting a scope (like a function). This does not account for indefinitely nesting scopes, e.g. recursive functions, but you are not working with those either.

The point is, you are working with an object and assigning a value to it. The value is changed, but there is no "old location". No memory leaks here.

Upvotes: 0

Zig Razor
Zig Razor

Reputation: 3495

No, it does not leak memory, this because the standard containers are programmed sanely. The assignement, in this case, just call the copy constructor of each element of the vector on the "right of =" and copy it on the vector on the "left of =". Before this kind of assignement the vector on the "left of =" call the destructor of all the object.

So if a memory leak happens it's due to the leak on the object argument of the vector template.

Upvotes: 1

Related Questions