Jarkid
Jarkid

Reputation: 181

How to delete a pointer before return it in a function?

If we have a function like this:

int* foo()
{
  int *x;
  x = new int;

  delete x;
  return x;
}

We need to return a pointer in a function, but what we have learned is we need to delete a space in the final.

If we delete x first as above, then is it meaningful to return x in the final line? Because the x does not exist anymore.

But if we don't delete x before return, the function will be finished.

How can we do? Or we don't really need to delete a space which was allocated in the memory?

Upvotes: 4

Views: 8747

Answers (4)

klutt
klutt

Reputation: 31389

There is absolutely no reason at all to delete the pointer before returning it. The only thing you will get is the memory address to a piece of memory that was allocated but no longer is.

It would make sense in this code:

main()
{
   int *ptr = foo()
   cout << "The memory address that function foo got "
        << "allocated to a local pointer is " << ptr << endl;
}

But come on, who would ever want to write such a thing?

It is, however perfectly ok to delete after the function call, like so:

int* ptr = foo();
delete ptr;

What you need to understand is that delete does not remove a pointer. What it does is to say "I (the program) am done with whatever this pointer is pointing to. You (the os kernel) can use it for anything now. I will not do anything more with that address. I promise." to the operating system. You do not have to have one delete for every pointer. This is completely ok, and causes no memory leaks:

  int *p=new int;
  int *a[3]={p, p, p};
  int n;
  cout << "Via which pointer in array a do you "
       << "want to delete the allocated memory? ";
  cin >> n;
  delete a[n];

It is pretty silly code, but it shows my point. It is not about the pointer. It's about what the pointer is pointing at.

And remember one thing. A pointer does not get the value NULL when you delete it. You have to take care of that separately.

Upvotes: 1

Yousaf
Yousaf

Reputation: 29282

if we delete x first as above, then is it meaningful to return x in the final line? because the x does not exist anymore.

Two things here, first no it is not meaningful to return x after delete x; and secondly, deleting x won't delete the x itself, it will only free up the memory to which the x points to.

or we don't really need to delete a space which was allocated in the memory?

Wrong. You need to free up every dynamically allocated memory location.

but if we don't delete x before return, the function will be finished. how can we do ?

What you can do is declare the pointer outside the function and then after you have returned the pointer x from the function, then you can delete it anywhere outside that function which returned the pointer.

Tip: Consider using Smart Pointers because along with other benefits, one of the biggest benefit of using Smart Pointers is that they free up the allocated memory automatically and save you the headache of freeing up the memory explicitly.

Upvotes: 2

spacm
spacm

Reputation: 300

Variables declared in the scope (between the {} ) of the functions are distroyed when leaving the function, as they are built on the stack memory of the function.

When using new(), memory is allocated on the heap, another memory space independant from the function.

This memory won't be freed before you delete the pointer pointing this memory, that's why once using a new(), you'll have to get the pointer deleted before your app returns.

Returning a deleted pointer doesn't make sense unless you want your application to crash. Dereferencing (accessing) a deleted pointer can lead to application crash or memory corruption.

If the purpose of your function is to provide a valid pointer on some type, the delete will have to occur outside of the function, once you wont need to use the variable anymore.

Two last tips:

  • don't delete a pointer twice, this would cause a crash.
  • assigning 0 to a deleted (or non allocated) pointer can be a guard:

ex:

int * pt = 0;

pt = new int;
*pt = 12;

delete pt;
pt=0;

if (pt==0) {
   // the pointer doesn't point to anything, and we can test it safely
}

/* ... */

if (pt!=0) {
    delete pt; //this second delete wont occur if pt is null (==0)
    pt=0;
}

Upvotes: 0

acraig5075
acraig5075

Reputation: 10756

You do need to delete the pointer at some stage, but that does not have to be in the same function scope where you new it. It can happen outside of your function, which I think is what you're trying to achieve.

int* foo()
{
  int *x;
  x = new int;
  return x;
}

int *px = foo();
// use px in some way
delete px;

Upvotes: 11

Related Questions