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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:44:52+00:00 2026-06-02T10:44:52+00:00

Question Can we use move semantics to move a heap allocated object on the

  • 0

Question

Can we use move semantics to move a heap allocated object on the stack?

Example

#include <boost/asio.hpp>
#include <memory>

class connection
{
public:
    connection(boost::asio::ip::tcp::socket&& socket);

    void start();

private:
    boost::asio::ip::tcp::socket m_socket;
};

class server
{
public:
    // Not relevent here

private:
    void accept();

    boost::asio::io_service        m_io_service;
    boost::asio::ip::tcp::acceptor m_acceptor;
};

void server::accept()
{
    auto socket = new boost::asio::ip::tcp::socket(m_io_service);

    m_acceptor.async_accept(*m_socket,
    [&, socket](const boost::system::error_code& error)
    {
        if (!error)
        {
            auto connection = std::make_shared<connection>(std::move(*socket));
            connection->start();
        }

        accept();
    });
}
  • 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-06-02T10:44:54+00:00Added an answer on June 2, 2026 at 10:44 am

    std::move does not relocate an object. It moves the value. (Think about a MOV R1, R2 instruction in some machine language: it does not move registers, just the contents! Or the memmove library function. It’s that sense of "move", not the kind of move that occurs in copying or compacting garbage collectors, which causes an object to reside at a different address while retaining its exact identity, which involves finding every single reference to the moved object everywhere in the machine and updating it).

    move semantics is a new feature in C++ which allows an object to be copied in a way that the old value does not have to be preserved. This is useful in cases when the old object is not going to be needed any more, because it can be faster.

    For instance, moving a vector from one std::vector<X> to another can cause the source object to be a zero-length vector, and all the data to move without any memory allocation or copying. Since the idea is that the old vector is not used, it doesn’t matter that it has been clobbered to zero length.

    There is no reason why this would not work between locations that are in different storage (e.g. free store ("heap") to automatic storage ("stack") given that copy construction between such operands has always worked fine in C++.

    [Edit 2020]

    However, if we move an object from the heap to somewhere else, we have a problem. The old memory in the heap was just abandoned without any destruction. That memory is no longer an object; to use it again we have to move or construct an object into it. Or else, we have should delete it (in a low-level way, without invoking a destructor).

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

Sidebar

Related Questions

I have a question that can I use AS400ConnectionPool in Spring if then pls
Brief question What command can I use to make my DataSet refresh it's connection
HTML Javascript question to get the selected value of a input-select I can use
Have a rather simple question. Does anyone knows if i can use jparallax both
I'd like to use the html5 widget + it's api (http://developers.soundcloud.com/docs/html5-widget) My question: can
Well, question is in title. Is it possible at all? I can use css
I asked a question earlier about why left joins in Linq can't use defined
My question is, why I can not use hide() & show() to hide/show table
I ask a similar question here and Darin Dimitrov answer that we can't use
This question may be an absolute crap. Can i use the flex 4 sdk

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.