Thread: move semantics

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    2

    move semantics

    Why do we need to set other._p_vals pointer to NULL? Once we set it to NULL, when the
    destructor of other is called, it will delete NULL, which is harmless, but it will not release the memory.
    Rvalue References and Move Semantics in C++11 - Cprogramming.com



    Code:
    
    
    Code:
    class ArrayWrapper
    {
    public:
        // default constructor produces a moderately sized array
        ArrayWrapper ()
            : _p_vals( new int[ 64 ] )
            , _size( 64 )
        {}
    
        ArrayWrapper (int n)
            : _p_vals( new int[ n ] )
            , _size( n )
        {}
    
        // move constructor
        ArrayWrapper (ArrayWrapper&& other)
            : _p_vals( other._p_vals  )
            , _size( other._size )
        {
            other._p_vals = NULL;
            other._size = 0;
        }
    
        // copy constructor
        ArrayWrapper (const ArrayWrapper& other)
            : _p_vals( new int[ other._size  ] )
            , _size( other._size )
        {
            for ( int i = 0; i < _size; ++i )
            {
                _p_vals[ i ] = other._p_vals[ i ];
            }
        }
        ~ArrayWrapper ()
        {
            delete [] _p_vals;
        }
    
    private:
        int *_p_vals;
        int _size;
    };
    
    

  2. #2
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    The destructor should try to delete the memory only when it is not nullptr, otherwise both destructors try to delete the same memory, which probably will crash the app.

    BTW. You don't need to bother about move constructor and move assignment operator if you use a vector instead of a pointer - see "Rule of Zero". C++ Core Guidelines: The Rule of Zero, Five, or Six - ModernesCpp.com

  3. #3
    Registered User
    Join Date
    Nov 2017
    Posts
    2
    yeah, you are right. The intention here is not to avoid memory leak, but to avoid deleting memory that will continue to be used.

    vector is helpful. But sometimes the class just need to manage some external resource, not necessary in array/vector format.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    But sometimes the class just need to manage some external resource,
    Then you really should be considering using smart pointers instead.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by OldGuy2 View Post
    The destructor should try to delete the memory only when it is not nullptr
    deleting a pointer equal to nullptr is a well-defined operation, and is explicitly allowed.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to take advantage of the Move Semantics (with code)?
    By Marco_I in forum C++ Programming
    Replies: 1
    Last Post: 02-28-2017, 03:54 AM
  2. How to take advantage of the move semantics?
    By Marco_I in forum C++ Programming
    Replies: 0
    Last Post: 02-27-2017, 08:40 AM
  3. Trouble with move semantics...
    By MutantJohn in forum C++ Programming
    Replies: 4
    Last Post: 11-16-2015, 08:30 PM
  4. move semantics / rvalue references
    By m37h0d in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2011, 01:58 PM
  5. Rvalue references and move semantics in C++11
    By webmaster in forum C++ Programming
    Replies: 14
    Last Post: 11-17-2011, 04:01 AM

Tags for this Thread