Question

I just wanted to know how one would go about, or if there is even a need to, delete [] an array declared like this:

int* arrgen(int x)
{
    int *newarray = new int[x];
    return newarray;
}

How would you go about deleting the newarray after the function returns? Or does it "auto" delete after the function returns along with the rest of the variable native to the function?

Was it helpful?

Solution

No, you would have to delete from outside the function in this case. Anyway, if you deleted from inside the function, you would have a dangling pointer when returning, so it makes no sense.

If you want automatic deallocation, do this, making use of c++11:

std::unique_ptr<int []> newarray(new int[x]);
return newarray;

This will return newarray and when you stop using it, it will call delete [] through the unique_ptr destructor.

OTHER TIPS

C++ gives you control of memory management (unlike garbage-collected languages like C# or Java which look after it for you). So, you can allocate memory with new (new[] for arrays) and de-allocate with delete (delete[] for arrays) as and when you see fit.

This can be more of a curse than a blessing - there are many ways it can go wrong. If you call 'delete' twice for the same address, you get undefined behaviour (e.g. program crash). If you allocate memory with new but fail to delete it, then you have a 'memory leak' - as your program runs it will grow and grow in memory usage.

To avoid these problems, you really need a strategy for managing memory. For every piece of memory that is allocated, it should be clear from your strategy which code 'owns' the memory and is responsible for deleting it. Two common strategies are smart pointers and RAII.

Your original code doesn't seem to have a strategy for ownership which means it could be dangerous. The code calling your function needs to assume ownership of the returned pointer and must be careful to delete it - possibly in lots of different places if your function is used widely. This explains why someone comments "just dont return allocated stuff", and why Germán suggests using a smart pointer.

How would you go about deleting the newarray after the function returns?

You wouldn't. Deleteing the new array when the function returns means to return an invalid reference/pointer to the array.

What you want is to use an std::vector:

std::vector<int> arrgen(int x) {
    std::vector<int> newarray(x);
    return newarray;
}

and return it (thanks to RVO you don't pay for any copy at all).

But hey, that's just the constructor of std::vector. So you don't even need to create a function for that:

std::vector<int> newarray(x);

You could make newarray static like this:

int* arrgen(int x) {
  static int *newarray = new int[x];
  return newarray;
}

This way the array doesn't get erased from the stack after exiting the function. Then inside main() you could do something like this:

int* vector = arrgen(n);
...
delete [] vector;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top