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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:23:54+00:00 2026-06-02T06:23:54+00:00

trying to test out move semantics i build a simple class that allocates some

  • 0

trying to test out move semantics i build a simple class that allocates some memory:

class CTest
{
    private:
        std::size_t size;
        char* buffer;

    public:
        CTest(std::size_t size) : size(size), buffer(nullptr)
        {
            buffer = new char[size];
        }

        ~CTest()
        {
            delete[] buffer;
        }

        CTest(const CTest& other) : size(other.size), buffer(nullptr)
        {
            buffer = new char[size];
        }

        CTest(CTest&& other) : size(other.size), buffer(other.buffer)
        {
            other.buffer = nullptr;
        }
};

as you can see its a very simple class that when it copies by ref it allocates new memory (doesnt copy its contents tho, just for testing).
and the move constructor just makes the internal pointer pointing to the argument data, no new memory is allocated.

for benchmarking im using windows QueryPerformanceCounter() with a helper function like:

template <typename Func>
__int64 Benchmark(Func f, size_t count)
{
    LARGE_INTEGER li = {};
    QueryPerformanceCounter(&li);
    __int64 start = li.QuadPart;

    f(count);

    QueryPerformanceCounter(&li);
    return li.QuadPart - start;
}

very simple benchmark function, saves the starting time and subtracts it from the ending time and returns the result.
now for the testing functions:

void Alloc_Ref(size_t count)
{
    CTest t(1024);
    for(size_t i = 0; i < count; ++i)
        CTest c(t);
}

void Alloc_Move(size_t count)
{
    for(size_t i = 0; i < count; ++i)
        CTest c(CTest(1024));
}

the Alloc_Ref one uses a pre-initialized variable so it calls the copy constructor and the Alloc_Move one simply uses a temporary class, so it calls the move constructor.

im calling the test like this:

cout << "Ref: " << Benchmark(Alloc_Ref, 1000000) << " ms." << endl;
cout << "Move: " << Benchmark(Alloc_Move, 1000000) << " ms." << endl;

problem is, Alloc_Move is not calling the move constructor, it continues to call the copy constructor, is there something im missing?

also equally important, when i do this on the Alloc_Move: CTest c(move(CTest(1024)) it does call the move constructor BUT its slower than the Alloc_Ref one, again is there something im missing?

sorry the long post and thanks in advance.

  • 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-02T06:23:56+00:00Added an answer on June 2, 2026 at 6:23 am

    In order to take advantage of move semantics, you need to explicitly use rvalue references. For compatibility with older code, the compiler will never implicitly cast to an rvalue reference for you; you need to either use std::move or explicitly cast.

    Your Alloc_Move is most likely slower because you’re constructing a new CTest(1024) each time you construct c, where as Alloc_Ref only constructs a single CTest(1024) and reuses it.

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

Sidebar

Related Questions

I am trying to test out some Dapper stuff using LinqPad. Dapper needs a
I'm trying to figure out how to test-drive software that launches external processes that
I'm trying to work out why some of my test cases (using RhinoMocks 3.6
I am currently trying to figure out how to do move semantics correctly with
I'm trying to test out some XPaths using the Scrapy shell, but it seems
I'm trying to test out some different methods of drawing to a Canvas, without
I'm trying to test out the GettingStarted sample site with our LE instance but
I'm trying to test this out on my site but it doesn't quite work
When trying to start my JUnit-Test out of Eclipse, I get a ClassNotFoundException. When
Trying to figure out how to adequately test my accounts controller. I am having

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.