Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7168027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:41:47+00:00 2026-05-28T14:41:47+00:00

I am currently trying to figure out how to do move semantics correctly with

  • 0

I am currently trying to figure out how to do move semantics correctly with an object which contains a pointer to allocated memory. I have a big datastructure, which contains an internal raw pointer to the actual storage (for efficiency reasons). Now I added a move constructor and move operator=(). In these methods I am std::move()ing the pointer to the new structure. However I am not sure what to do with the pointer from the other structure.

Here is a simple example of what I am doing:

class big_and_complicated {
   // lots of complicated code
};

class structure {
public:
   structure() :
      m_data( new big_and_complicated() )
   {}

   structure( structure && rhs ) :
      m_data( std::move( rhs.m_data ) )
   {
      // Maybe do something to rhs here?
   }

   ~structure()
   {
      delete m_data;
   }

private:
   big_and_complicated * m_data;
}

int main() {
  structure s1;
  structure s2( std::move( s1 ) );
  return 0;
}

Now from what I understand, after the std::move( s1 ) to s2 the only thing that is safe to do on s1 ist to call its constructor. However as far as I can see, this would lead to deleting the pointer contained within s1 in the destructor, rendering s2 useless as well. So I am guessing I have to do something to render the destructor safe when std::move()ing the pointer. As far as I can see the safest thing to do here, is to set it to 0 in the moved object, since this would turn the delete into a no-op later on. Is this reasoning correct so far? Or is std::move() actually smart enough to null out the pointer for me, rendering its usage safe? So far I am seeing no crashes in the actual test-suite, but I have not made sure the move-constructor is actually called.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:41:48+00:00Added an answer on May 28, 2026 at 2:41 pm

    "Moving" a pointer is no different than copying one and does not set the moved-from value to null (‘moving’ is in quotes here because std::move does not move anything really, it just changes the value category of the argument). Just copy rhs‘s pointer then set it to nullptr:

    struct structure
    {
        structure()
          : m_data{new big_and_complicated{}}
        { }
    
        structure(structure&& rhs)
          : m_data{rhs.m_data}
        {
            rhs.m_data = nullptr;
        }
    
        structure& operator =(structure&& rhs)
        {
            if (this != &rhs)
            {
                delete m_data;
                m_data = rhs.m_data;
                rhs.m_data = nullptr;
            }
            return *this;
        }
    
        ~structure()
        {
            delete m_data;
        }
    
    private:
        big_and_complicated* m_data;
    
        structure(structure const&) = delete;             // for exposition only
        structure& operator =(structure const&) = delete; // for exposition only
    }
    

    You could also simplify this by using std::exchange:

    structure(structure&& rhs)
        : m_data{ std::exchange(rhs.m_data, nullptr) }
    { }
    
    structure& operator=(structure&& rhs)
    {
        if (this != &rhs)
        {
            delete m_data;
            m_data = std::exchange(rhs.m_data, nullptr);
        }
        return *this;
    }
    

    Better yet, use std::unique_ptr<big_and_complicated> instead of big_and_complicated* and you don’t need to define any of this yourself:

    #include <memory>
    
    struct structure
    {
        structure()
          : m_data{new big_and_complicated{}}
        { }
    
    private:
        std::unique_ptr<big_and_complicated> m_data;
    }
    

    Lastly, unless you actually want structure to remain non-copyable, you’re better off just implementing proper move semantics inside of big_and_complicated and having structure hold a big_and_complicated object directly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to figure out the membership module in asp.net. Currently i have a
I'm currently trying to figure out which implementation of JSR-311 I'm going to recommend
I'm currently beating my head against a wall trying to figure this out. But
I'm trying to figure out how to use Zend_Db_Table_Abstract correctly. I want to return
I am currently trying to figure out what the best way is to create
I am currently trying to figure out the best way to create a .xls
I'm trying to figure out the best build system for latex. Currently, I use
I am currently trying to figure out how to save the content from a
I am currently trying to figure out how to use Eclipse to program Escape
I am currently trying to figure out a way to write a media file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.