Why does the use of 'new' cause memory leaks?

后端 未结 9 1019
走了就别回头了
走了就别回头了 2020-11-22 08:25

I learned C# first, and now I\'m starting with C++. As I understand, operator new in C++ is not similar to the one in C#.

Can you explain the reason of

相关标签:
9条回答
  • 2020-11-22 09:03

    Given two "objects":

    obj a;
    obj b;
    

    They won't occupy the same location in memory. In other words, &a != &b

    Assigning the value of one to the other won't change their location, but it will change their contents:

    obj a;
    obj b = a;
    //a == b, but &a != &b
    

    Intuitively, pointer "objects" work the same way:

    obj *a;
    obj *b = a;
    //a == b, but &a != &b
    

    Now, let's look at your example:

    A *object1 = new A();
    

    This is assigning the value of new A() to object1. The value is a pointer, meaning object1 == new A(), but &object1 != &(new A()). (Note that this example is not valid code, it is only for explanation)

    Because the value of the pointer is preserved, we can free the memory it points to: delete object1; Due to our rule, this behaves the same as delete (new A()); which has no leak.


    For you second example, you are copying the pointed-to object. The value is the contents of that object, not the actual pointer. As in every other case, &object2 != &*(new A()).

    B object2 = *(new B());
    

    We have lost the pointer to the allocated memory, and thus we cannot free it. delete &object2; may seem like it would work, but because &object2 != &*(new A()), it is not equivalent to delete (new A()) and so invalid.

    0 讨论(0)
  • 2020-11-22 09:03
    B object2 = *(new B());
    

    This line is the cause of the leak. Let's pick this apart a bit..

    object2 is a variable of type B, stored at say address 1 (Yes, I'm picking arbitrary numbers here). On the right side, you've asked for a new B, or a pointer to an object of type B. The program gladly gives this to you and assigns your new B to address 2 and also creates a pointer in address 3. Now, the only way to access the data in address 2 is via the pointer in address 3. Next, you dereferenced the pointer using * to get the data that the pointer is pointing to (the data in address 2). This effectively creates a copy of that data and assigns it to object2, assigned in address 1. Remember, it's a COPY, not the original.

    Now, here's the problem:

    You never actually stored that pointer anywhere you can use it! Once this assignment is finished, the pointer (memory in address3, which you used to access address2) is out of scope and beyond your reach! You can no longer call delete on it and therefore cannot clean up the memory in address2. What you are left with is a copy of the data from address2 in address1. Two of the same things sitting in memory. One you can access, the other you can't (because you lost the path to it). That's why this is a memory leak.

    I would suggest coming from your C# background that you read up a lot on how pointers in C++ work. They are an advanced topic and can take some time to grasp, but their use will be invaluable to you.

    0 讨论(0)
  • 2020-11-22 09:05

    If it makes it easier, think of computer memory as being like a hotel and programs are customers who hire rooms when they need them.

    The way this hotel works is that you book a room and tell the porter when you are leaving.

    If you program books a room and leaves without telling the porter the porter will think that the room is still is use and will not let anyone else use it. In this case there is a room leak.

    If your program allocates memory and does not delete it (it merely stops using it) then the computer thinks that the memory is still in use and will not allow anyone else to use it. This is a memory leak.

    This is not an exact analogy but it might help.

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