Question

I'm studying c++ and I'm reading about pointers. I'm curious about the following scenarios:

Scenario 1:

If I'm not mistaken, if the user types -1, there will be a memory leak:

#include <iostream>
using namespace std;

int main(){

    int *p = new int;

    cout << "Please enter a number: ";
    cin >> *p;

    if (*p == -1){
        cout << "Exiting...";
        return 0;
    }

    cout << "You entered: " << *p << endl;

    delete p;

    return 0;
}

Scenario 2:

But what happens in the following code? From what I've read and correct me if I'm wrong, when declaring a pointer like in the second scenario the pointer gets cleared out once you are out of scope. So if the user doesn't enter -1, the *p will be auto-cleared?

#include <iostream>
using namespace std;

int main(){

    int x;
    int *p = &x;

    cout << "Please enter a number: ";
    cin >> *p;

    if (*p == -1){
        cout << "Exiting...";
        return 0;
    }

    cout << "You entered: " << *p << endl;

    return 0;
}

What happens if I enter -1 in the second scenario?

Was it helpful?

Solution

Do not focus on the fact that you are using pointers that much. Memory leaks are usually about memory that the pointer points to, not about the pointer itself.

In this code:

int x;
int *p = &x;

there is no memory leak since there is no memory that would require explicit deallocation (no memory that has been allocated dynamically). int x is a variable with automatic storage duration that will be cleaned up automatically when the execution goes out of scope and int *p = &x; is just a pointer that holds the address of the memory where x resides.


But you are right that in code like:

Resource* r = new Resource();

if (something) {
    return -1;
}

delete r;

there is a memory leak since there is a return path (exit path) that doesn't free the allocated memory. Note that the same would happen if the exception would be thrown instead of return being called... ensuring that all resources are freed properly is one of the main reasons why you should learn more about smart pointers, the RAII idiom and try to prefer objects with automatic storage duration over dynamically allocated ones.

OTHER TIPS

In the second scenario, everything's fine.

Indeed, you haven't allocated memory (while you did in the first scenario). In the first case, the pointer "holds" the memory you allocated through new. In the second case, it points to a local variable with automatic-storage duration (i.e. it will be removed when going out of scope).

Simple rule: If you used new you must use delete (unless you're using a smart pointer). In the first scenario, if you type -1, you end up with one new and zero delete, which yields a memory leak. In the second case, you haven't allocated anything, the pointer points to memory that is already managed.

In the second scenario you do not allocate memory so you should not bother about some memory leak. You have to use delete or delete[] if you explicitly allocate memory with new or new for arrays.

In the second scenario p points to local variable x. It is the compiler that allocated memory for x in stack. So it is the compiler that properly will free this memory after x will be out of its scope.

In the first main, you dynamically allocating the int and manually delete it when exiting from main. However, if int is equal to -1, you do not delete it and return so that's why you have a memory leak.

In second example, the int is allocated on the stack and you're taking the address of it. When main returns, the int is deallocated automatically. If you tried to call delete on it in the main, you would get a crash.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top