0
void test(int pnt)
{ 
  int* p = &pnt; //can a pointer get deallocated?
  return;
}

Can pointers get deleted in this case? I don't know if they do. But I do know that if you get a pointer from the free store, it won't get deallocated.

  • The pointer itself is an automatic variable and it's memory will be freed at the end of the scope. If you do: `{ int* p = new int; }` then the memory for `p` will be released but not the `int` it pointed at. In your function however, `pnt` is also automatic and will be freed automatically. – Ted Lyngmo Jun 29 '20 at 22:00
  • @TedLyngmo so if p points to ```new int``` then would p get deleted but not the ```new int```? –  Jun 29 '20 at 22:03
  • You only `delete` objects created with `new`. Every use of `delete` must match with exactly one use of `new`. No object was created with `new` here, so there is nothing that needs to be `delete`d. You do not `delete` pointers; you `delete` the object pointed to by a pointer. – Miles Budnek Jun 29 '20 at 22:03
  • @doggo _Deleted_ is maybe not the right word but the memory `p` occupies will be released but not the memory the `int` occupies. – Ted Lyngmo Jun 29 '20 at 22:05
  • The pointer is just a plain, old regular variable with the usual lifetime rules. Its value just happens to be the address of some other object (or nothing and there is no way for the pointer to know for sure). – user4581301 Jun 29 '20 at 22:07
  • @TedLyngmo oh, ok. I mixed up 'Deleted' and 'Released'. Makes sense. –  Jun 29 '20 at 22:07
  • Does this answer your question? [Deleting a pointer in C++](https://stackoverflow.com/questions/13223399/deleting-a-pointer-in-c) – Aykhan Hagverdili Jun 29 '20 at 22:21

1 Answers1

4

The pointer p is a local variable in the test function so it gets deallocated when the function returns.

The argument pnt is also a local variable in the test function so it gets deallocated when the function returns.

If you returned &pnt from the function, it would be a dangling pointer after the function returned, because the variable pnt would have been destroyed.

int* test(int pnt) {
    return &pnt; // bad idea, but technically you haven't done anything wrong yet
}

void test2() {
    int* p = test(5);
    cout << *p; // accesses a deallocated variable. Bad stuff happens here
}

If you returned p, and p did not point to a local variable, that is fine, because the thing it points to does not get destroyed, and the pointer variable itself is not relevant, as it only holds the pointer temporarily:

int pnt = 7; // global variable - doesn't get deallocated
int* test() {
    int* p = &pnt;
    return p; // this is fine. p is deallocated but we don't need p any more anyway
}

void test2() {
    int* p = test();
    cout << *p; // still fine, it prints 7
}
user253751
  • 57,427
  • 7
  • 48
  • 90
  • Why is there a 5 in your second code block [int*p=test(5)]? Should it not be there since test() does not take any parameters in your second code block? I'm trying to better understand pointers and memory allocation. This answer helped. I was just confused about the 5. – EITB Jan 19 '21 at 04:11