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 3310006
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:42:29+00:00 2026-05-17T21:42:29+00:00

I’m confused about the state of an object after it’s been moved using C++0x

  • 0

I’m confused about the state of an object after it’s been moved using C++0x move semantics. My understanding is that once an object has been moved, it’s still a valid object, but its internal state has been altered so that when its destructor is called, no resources are deallocated.

But if my understanding is correct, the destructor of a moved object should still be called.

But, that doesn’t happen when I perform a simple test:

struct Foo
{
    Foo()  
    {
        s = new char[100]; 
        cout << "Constructor called!" << endl;  
    }

    Foo(Foo&& f) 
    {
        s = f.s;
        f.s = 0;
    }

    ~Foo() 
    { 
        cout << "Destructor called!" << endl;   
        delete[] s; // okay if s is NULL
    }

    void dosomething() { cout << "Doing something..." << endl; }

    char* s;
};

void work(Foo&& f2)
{
    f2.dosomething();
}

int main()
{
    Foo f1;
    work(std::move(f1));
}

This output:

Constructor called!
Doing something...
Destructor called!

Notice the destructor is only called once. This shows that my understanding here is off. Why wasn’t the destructor called twice? Here’s my interpretation of what should have happened:

  1. Foo f1 is constructed.
  2. Foo f1 is passed to work, which
    takes an rvalue f2.
  3. The move constructor of Foo is
    called, moving all resources in f1
    to f2.
  4. Now f2‘s destructor is called,
    releasing all resources.
  5. Now f1‘s destructor is called,
    which doesn’t actually do anything
    since all resources were transferred
    to f2. Still, the destructor is
    called nonetheless.

But since only one destructor is called, either step 4 or step 5 isn’t happening. I did a backtrace from the destructor to see where it was being invoked from, and it’s being invoked from step 5. So why isn’t f2‘s destructor also called?

EDIT: Okay, I modified this so it’s actually managing a resource. (An internal memory buffer.) Still, I get the same behavior where the destructor is only called once.

  • 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-17T21:42:30+00:00Added an answer on May 17, 2026 at 9:42 pm

    Edit (New and correct answer)
    Sorry, looking closer at the code, it seems the answer is much simpler:
    you never invoke the move constructor. You never actually move the object. You just pass a rvalue reference to the work function, which calls a member function on that reference, which still points back to the original object.

    Original answer, saved for posterity

    In order to actually perform the move, you have to have something like Foo f3(std::move(f2)); inside work. Then you can call your member function on f3 which is a new object, created by moving from f

    As far as I can see, you don’t get move semantics at all. You’re just seeing plain old copy elision.

    for the move to happen, you have to use std::move (or specifically, the argument being passed to the constructor has to be an unnamed/temporary) rvalue reference, such as the one that is returned from std::move). Otherwise it is treated as a plain old-fashioned lvalue reference, and then a copy should happen, but as usual, the compiler is allowed to optimize it away, leaving you with one object being constructed, and one object being destroyed.

    Anyway, even with move semantics, there’s no reason why the compiler shouldn’t do the same thing: just optimize the move, just like it’d have optimized away the copy. A move is cheap, but it’s still cheaper to just construct the object where you need it, rather than constructing one, and then moving it into another location and calling the destructor on the first one.

    It’s also worth noting that you’re using a relatively old compiler, and earlier versions of the spec were very unclear on what should happen with these “zombie objects”. So it is possible that GCC 4.3 just doesn’t call the destructor. I believe it’s only the last revision, or maybe the one before it, that explicitly requires the destructor to be called

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I have a French site that I want to parse, but am running into

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.