NULL pointer is the same as deallocating it?

前端 未结 11 2496
一个人的身影
一个人的身影 2021-02-18 18:29

I was working on a piece of code and I was attacked by a doubt: What happens to the memory allocated to a pointer if I assign NULL to that pointer?

For instance:

相关标签:
11条回答
  • 2021-02-18 18:51

    Variables stored on the stack, are the local variables of each function, e.g. int big[10]; Variables stored on the heap, are the variables which you initiated using explicit memory allocation routines such as malloc(), calloc(), new(), etc.

    Variables stored on the stack have a lifetime that is equal to the lifetime of the current stack frame. In English, when the function returns, you can no longer assume that the variables hold what you expect them to hold. That's why its a classic mistake to return a variable that was declared local in a function.

    0 讨论(0)
  • 2021-02-18 18:52

    As per Phil Nash's comment, for every new, there is a corresponding delete, likewise, for every malloc, there is a corresponding free. If the corresponding delete/free is not there, you have a leak.

    Hope this helps, Best regards, Tom.

    0 讨论(0)
  • 2021-02-18 18:54

    On most modern OSs, the application's memory will be reclaimed at exiting the application. Meanwhile, you have a memory leak.

    0 讨论(0)
  • 2021-02-18 18:54

    No, it will be lost to the process forever. You will have a memory leak. If you keep doing this, your program will eventually run out of memory!! To avoid this, delete the object when you no longer need it.

    Often people will set the pointer to NULL after deleting it, so that other parts of the program can verify that the object has been deleted, and thereby avoid accessing or deleting it again.

    0 讨论(0)
  • 2021-02-18 18:56
    A = new MyClass();
    
    {...do something in the meantime...}
    
    A = NULL;
    

    The way I keep track of it is that there are two separate objects. Somewhere on the heap, a MyClass instance is allocated by new. And on the stack, there is a pointer named A.

    A is just a pointer, there is nothing magical about out, and it doesn't have some special connection to the heap-allocated MyClass object. It just happens to point to that right now, but that can change.

    And on the last line, that is exactly what happens. You change the pointer to point to something else. That doesn't affect other objects. It doesn't affect the object it used to point to, and it doesn't affect the object (if any) that it is set to point to now. Again, A is just a dumb raw pointer like any other. It might be NULL, or it might point to an object on the stack, or it might point to an object on the heap, or it might be uninitialized and point to random garbage. But that's all it does. It points, it doesn't in any way take ownership of, or modify, the object it points to.

    0 讨论(0)
  • 2021-02-18 18:58

    You need to delete A;

    For regular objects setting the pointer to NULL does nothing but invalidating the pointer, the object is still around in memory, this is particularly true if you notice that you may have more than one pointer to the same object, changing one shouldn't affect the others.

    0 讨论(0)
提交回复
热议问题