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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:53:43+00:00 2026-06-01T04:53:43+00:00

I need a reference counter for an object not allocated on the heap. I

  • 0

I need a reference counter for an object not allocated on the heap.

I need it to implement a RAII mechanism on objects that cannot be easily copied and destructed:

class File
{
private:
    int fd;
public:
    File( const std::string &path ) ... // opening file
    destroy( );                         // actually closing file

    File( const File &f ) ...           // just copying the fd
    ~File( );                           // doing nothing
}

For a scenario like this a std::shared_ptr is usually used: the constructor and the destructor of the object whose pointer is shared are called only once.

In my case, however, I’d prefer avoiding allocating the object on the heap. I’d need a shared_object class that does a job similar to std::shared_ptr, so that my class’ non-copy-constructor and destroy function (in the example above) are called only once.

Does anything like this exist?

  • 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-01T04:53:44+00:00Added an answer on June 1, 2026 at 4:53 am

    If you want to have shared pointer behavior while allocating nothing in dynamic storage (“on the heap”) you can look at various smart pointer implementation strategies. In Modern C++ Design, the author discusses many of these strategies in the “Smart Pointers” chapter, which is freely (and legally) available online.

    The technique you will be interested in is reference linking. Using this technique, the smart pointer objects are linked together in a bi-directional doubly linked list instead of pointing to a dynamically allocated reference counter.


    All that being said, using a std::shared_ptr, std::unique_ptr or their Boost variants will probably be much faster to write and easier to maintain. If dynamic allocation and the reference count are ever a bottleneck (I doubt it will be, but then we can’t generalize too hastily), you can always take time to use a custom reference linking version instead.

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

Sidebar

Related Questions

Say I have some 10 categories that I need to reference in a web
I have a need to reference two different versions of the Sharepoint API dll.
I have a list of error codes I need to reference, kinda like this:
I am building a common function library but the functions inside need to reference
I'm creating some text boxes on my form programmatically which I need to reference
I'm building a Windows Phone 7 app, and I need a reference to my
Somebody ask me this question today . What is the need of reference in
I am writing a resume script and I need the heading Reference to be
What do the result codes in SVN mean? I need a quick reference.
How to get reference count for an object Is it possible to determine if

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.