4

Consider the code snippet in C++:

int *foo() {
    int *y = new int[1000];
    return y;
}

int main() {
    int *x = new int [1000];
    x = foo();
    delete[] x;
    return 0;
}

When x is created it points to a memory address. When foo() is called, a new pointer y is created, pointing to a different address, but x is then set to the address that y had. So when it is deleted, the new address's memory is freed, but the original address that x had is leaked. Is that correct?

Furthermore, I made a slight change to the snippet by calling delete[] x before calling foo() and it still compiled and ran:

int *foo() {
    int *y = new int[1000];
    return y;
}

int main() {
    int *x = new int [1000];
    delete[] x;
    x = foo();
    delete[] x;
    return 0;
}

Does this mean I prevented the leak? And one final question, if I do not initialize x when I declare it, but do not prematurely delete it, is it pointing to memory that will be leaked? Like below:

int *foo() {
    int *y = new int[1000];
    return y;
}

int main() {
    int *x;
    x = foo();
    delete[] x;
    return 0;
}

As a side note, I understand that using a vector and/or unique pointers is safer, but I'm curious as to the functionality of the code above, specifically what happens when a pointer is declared but initialized later.

1
  • Well formed question; thank you! Commented Feb 23, 2019 at 1:34

2 Answers 2

4

When x is created it points to a memory address. When foo() is called, a new pointer y is created, pointing to a different address, but x is then set to the address that y had. So when it is deleted, the new address's memory is freed, but the original address that x had is leaked. Is that correct?

Yes.

Does this mean I prevented the leak?

Yes.

And one final question, if I do not initialize x when I declare it, but do not prematurely delete it, is it pointing to memory that will be leaked?

No.

As a side note, I understand that using a vector and/or unique pointers is safer

Absolutely. Even without those your code could be a lot clearer about memory ownership such that your question wouldn't even arise, but it's true that switching to standard containers/smart pointers would solve the problem right at the source, and I strongly recommend this.


tl;dr: you're right about everything so far

Sign up to request clarification or add additional context in comments.

4 Comments

So the way I understand it, an uninitialized pointer doesn't really point to anything yet.
@Daniel Pointers gonna point. An uninitialized pointer points somewhere, but it does not point at anything safe for you to use. In a modern system it usually points at instant death and the Grim Garbage Collector is all too happy to reap. But sometimes it winds up pointing at memory that has been assigned to the program, and these can be an absolute <expletive deleted> to find and debug. The code over here that has nothing to do with the code over there just mulched over there's memory and when the program screws up over there you probably won't start by debugging the code over here.
@Daniel Correct.
@user4581301 "Pointers gonna point. An uninitialized pointer points somewhere, but it does not point at anything safe for you to use" That's a matter of semantics. It is easy to argue that an uninitialised pointer indeed does not "point somewhere", since you cannot observe its value without UB (its value is unspecified). In practice it has some arbitrary numerical value that you could describe as "pointing somewhere possibly unsafe" but that's not really how the language sees it, and that's not how you should see it either.
0

In your snippet above:

int *x = new int [1000];
x = foo();
delete[] x;

You allocated an array pointer to x, but then "squished" it by assigning foo() to it. If you were working in a garbage collected language (or using a smart pointer), the memory would have been reclaimed in that case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.