Why delete is needed in the definition of the copy-assignment operator?

前端 未结 4 1586
面向向阳花
面向向阳花 2021-01-13 15:09

I am a C++ beginner. And I am doing the exercises in C++ Primer (5th Edition). I found a reference to Exercise 13.8 from Github (Here), which is shown below.



        
相关标签:
4条回答
  • 2021-01-13 15:28

    HasPtr::ps is an heap-allocated std::string pointer.

    It is allocated and constructed using new in all HasPtr constructors. Therefore, when HasPtr::ps gets replaced by another heap-allocated pointer, the existing memory has to be released using delete to avoid memory leaks.

    Note that, in modern C++, you should almost never use new and delete for managing objects like this. Use smart pointers instead, like std::unique_ptr or std::shared_ptr, which take care of memory management for you, safely and conveniently.

    I still suggest getting familiar with new and delete, as an huge amount of existing code makes use of them. cppreference.com is a great place to find detailed information about the language.

    As Jan Hudec mentioned in the comments, it is generally pretty silly to store an std::string on the heap - std::string is a wrapper around an heap-allocated character array, which already manages the memory for you.

    0 讨论(0)
  • 2021-01-13 15:37

    The delete is needed to avoid a memory leak.

    The line ps = new_ps; edits the address of ps to point somewhere else.

    The memory that ps was previously pointing at still needs to be freed. The effects of removing this line are not instantly visible and the program will still 'work', but you have a memory leak.

    Eg.

    ps = address 0 with value 'f'; new_ps = address 1 with value 'g'
    Now let ps = new_ps;
    ps = address 1 with value 'g'; new_ps = address 1 with value 'g'
    So address 0 is no longer something we can access, but it's not been freed either
    
    0 讨论(0)
  • 2021-01-13 15:42

    It's needed to prevent a memory leak: every new must be balanced with a delete.

    You allocate memory for ps with a new in the constructors.

    When you allocate memory for new_ps and assign this to ps, you need to free the constructor-allocated memory before you lose the old pointer value.

    You program will indeed "work" if you omit that line, but it will steadily consume more and more memory, until, eventually there is no more memory left.

    Note that you have another memory leak: you need to create a destructor, and call delete ps in it.

    As you can see, this is getting unduly complicated. Better still, ditch all this pointer stuff and use a std::string s as the member variable - it takes care of all the memory management for you.

    0 讨论(0)
  • In C++ programming, calling delete on a class object automatically invokes the destructor of the class.

    In general practice, destructor holds the code to free up the resources i.e file pointers,deletion of objects created inside the class.This way, when you call delete on an object,the resources it allocated are freed up i.e given back to the system

    If these resources are not freed up,the memory allocated for these resources will not be given back to the system.Every time your object gets called,you lose a certain portion of memory and over a period of time you would have lost a major chunk of memory.This is called as memory leak.

    Thus when you call delete on an object,you ensure that the resources you acquired are released, provided you have done those operations in destructor.

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