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

  • Home
  • SEARCH
  • 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 6701197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:54:18+00:00 2026-05-26T06:54:18+00:00

I have a simple reference counted class that holds a memory buffer. It looks

  • 0

I have a simple reference counted class that holds a memory buffer. It looks like this:

#include <algorithm>

template<typename T>
struct buffer
{
    // create a buffer of length n
    buffer(unsigned n) : rc(*(new unsigned(1))), data(new T[n]) { }

    buffer(const buffer<T> & rhs) : rc(++rhs.rc), data(rhs.data) { }

    buffer<T>& operator=(buffer<T> rhs)
    {
        std::swap(rc, rhs.rc);
        std::swap(data, rhs.data);
        return *this;
    }

    ~buffer()
    {
        if (--rc == 0) {
            delete [] data;
            delete (&rc);
        }
    }

private:
    mutable unsigned & rc;
    T * data;
};


int main() {
    typedef buffer<int> numbers;
    numbers n1(10);
    numbers n2(20);
    numbers n3(30);
    n1 = n2 = n3 = n2;
}

I don’t see anything wrong with the code. However Visual Studio and valgrind complain about memory corruption.

I’ve stared at this code for way too long now. Can anyone spot the error?

  • 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-26T06:54:19+00:00Added an answer on May 26, 2026 at 6:54 am

    One problem is that when you swap(rc, rhs.rc); you’re actually swapping the contents of the recounts, not the references.

    Imagine you have this situation:

    Before

    When you swap(rc, rhs.rc);, the references to the refcounts will remain the same, and the counts themselves will swap. This is what you get after the two swaps:

    After

    The buffer pointers are corrrect, but the references to the refcounts still refer to the same unsigned objects. The value of these objects was swapped though. Notice how the buffer B has refcount 2 when seen from one of the swapped objects, and refcount 1 when seen from the one on the bottom.

    You need to use pointers for the refcount, and swap the pointers, not the contents.

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

Sidebar

Related Questions

I have simple HTML code with some JavaScript. It looks like: <html> <head> <script
i have this simple class: class A { var $children=array(); function &__get($name) { if($name===firstChild)
I have a simple MVC application that sometimes fails in production. I would like
When Connected: I have a simple HTML page with some image elements that reference
I'd like to create a simple reference app that lists a group of people,
I have simple win service, that executes few tasks periodically. How should I pass
I have a simple table for reference page: id name description image In reference.php,
I have a simple WCF service that I call server side from code behind
I have a simple table that logs simple events an administrator does. Nothing complicated.
I have some simple .NET objects I'd like to serialize to JSON and back

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.