简体   繁体   中英

Can I force std::vector to leave a memory leak?

Can I force std::vector to not deallocate its memory after the vector goes out of scope?

For example, if I have

int* foo() {
    std::vector<int> v(10,1); // trivial vector
    return &v[0];
}

int main()
{
    int* bar = foo();
    std::cout << bar[5] << std::endl;
}

There is no guarantee that the values will still be accessible here.

I am currently simply doing this

int* foo() {
  std::vector<int> v(10,1);
  int* w = new int[10];
  for (int i=0; i<10; i++) {
    w[i] = v[i];
  }
  return w;
}

but it is a little wasteful to repopulate a whole new array. Is there a way to force std::vector to not delete its array?

Note: I am not returning the vector itself because I am interfacing c++ with python using SWIG, and ARG_OUTVIEW_ARRAY requires a raw pointer and, in fact, an intentional memory leak. I would still however like to be able to make use of vector features while constructing the data itself.

It is possible but you should never do it. Forcing a vector to leave memory leak is a terrible idea and if you need such a thing then you need to re-think your design. std::vector is a resource managing type whose one of the main goals is to ensure that we don't have a leak. Never try to break that.

Now, to answer your specific question: std::vector takes an allocator type as second template parameter which is default to std::allocator<T> . Now you can write a custom allocator that doesn't release any memory and use that with your vector. Writing a custom allocator is not very trivial work, so I'm not going to describe that here (but you can Google to find the tutorials).

If you really want to use custom allocator then you must ensure that your vector never triggers a grow operation. Cause during growing capacity the vector will move/copy data to new location and release the old memories using the allocator. If you use an allocator that leaks then during growing you not only retain the final data, but also retain the old memories which I'm sure that you don't want to retain. So make sure that you create the vector with full capacity.

No.

Vectors are not implemented to have memory leaks, and the interface does not provide a way to create one.

You can't "steal" the memory (removing ownership of it from the vector), which is possibly a bit of a shame.

Sorry, but you are going to have to either copy (as you're doing now), or not use vector.

The vector is desiged to prevent leaks.

But if you want to shoot yourself in the foot, it's possible. Here's how you prevent the vector from deallocating its internal array:

int *foo()
{
    std::vector<int> v(10,1);

    int *ret = v.data();
    new (&v) std::vector<int>; // Replace `v` with an empty vector. Old storage is leaked.
    return ret;
}

As the other answers say, you should never do it.

This is a bad idea, but possible by creating a custom allocator that does not deallocate as said in other answers.

For example : (boilerplate mostly from cppref )

#include <cstdlib>
#include <new>
#include <vector>

template <typename T>
struct LeakingAllocator 
{
  using value_type = T;
  LeakingAllocator() = default;

  template <typename U> constexpr LeakingAllocator(const LeakingAllocator<U>&) noexcept {}

  T* allocate(std::size_t n) 
  {
    if(n > std::size_t(-1) / sizeof(T)) throw std::bad_alloc(); // check for overflow

    if(auto p = static_cast<T*>(std::malloc(n*sizeof(T)))) return p; // return p if malloc returns a valid object
    throw std::bad_alloc(); // otherwise just throw.
  }

  void deallocate(T* p, std::size_t) noexcept { /*leak intentionally*/ }
};


template <typename T, typename U>
bool operator==(const LeakingAllocator<T>&, const LeakingAllocator<U>&) { return true; }
template <typename T, typename U>
bool operator!=(const LeakingAllocator<T>&, const LeakingAllocator<U>&) { return false; }

template <typename T>
using LeakingVector = std::vector<T, LeakingAllocator<T>>;

Then code like

int* ret()
{
    LeakingVector<int> a;
    a.resize(10);
    return &a[0];
}

int main()
{
    auto ptr = ret();
    *ptr = 10;
    std::cout << *ptr;
}

becomes valid.

Not sure but, yes.

You can create a custum allocator who do nothing when deallocate => leak

Or may be you can jsut create your vector on the heap so it will leak anyway.

int* foo() {
    std::vector<int>* v = new std::vector<int>(10,1); 
    return &((*v)[0]);
    // no delete
}

int main()
{
    int* bar = foo();
    std::cout << bar[5] << std::endl;
}

No.

And you're doing it wrong. Return the vector instead so the lifetime works out:

Write your own special Python memory vector class , something like (most crudely):

template <typename T>
class python_vector
{
    T* buffer_;
public:
    python_vector(size_t n, const T& value) : buffer_{new T(n)}
    {}
    // copy, assignment, operator[](), *etc*
    ~python_vector()
    {
        // DO NOTHING!
    }
}

python_vector<int> foo() {      
    python_vector<int> v(10,1);
    // process v
    return v;
}

int main()
{
    python_vector<int> bar = foo();  // copy allusion will build only one python_vector here
    std::cout << bar[5] << std::endl;
}

In C++ you would most probably write:

auto foo()
{
    std::vector<int> v(10,1); // trivial vector
    return v;
}

int main()
{
    const auto bar = foo();
    std::cout << bar[5] << std::endl;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM