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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:11:43+00:00 2026-05-29T16:11:43+00:00

I am using C++ do represent large graphs using reference counted objects. Something like:

  • 0

I am using C++ do represent large graphs using reference counted objects. Something like:

// Public class
class Pointer{
  public:
  // Constructors
  Pointer(...){ node = ...; node->counter++;}

  // Copy constructor
  Pointer(Pointer& const other){ node = other.node; node->counter++;}

  // Destructor
  ~Point(){ if(--node->counter==0) delete node;}

  // Other methods
  ...

  Node* node;
};

// Node base class
class Node{
  // Constructor
  Node():counter(0){}

  // Destructor
  virtual ~Node(){}

  // public:
  unsigned long counter;
};

// Node derived class
class NodeDerived : public Node{
  // Constructors and other methods
  ...

  // Destructor
  virtual ~NodeDerived(){ ... }

  // Children
  Pointer children[2];
};

There is thus a public class Pointer which contains a pointer to a polymorphic class Node. Some classes derived from Node will contain new Pointer instances. Note that several Pointer instances may point to the same Node instance.

I use this class to build up very large graphs with millions of instances, and this works fine. The problem comes when the graph is to be deleted. With the default destructor implementation this results in a stack overflow, since in the destructor of the root node, the destructors of the children are called and in the children’s destructors, the destructors of the children of the children are called and so on.

I solved this by implementing a destructor for the NodeDerived class above that avoids the recursive call by using a stack instead. This works, but is still very slow, and I am looking for a way to speed this up. Is it possible to somehow avoid declaring the destructor virtual without causing a memory leak?

  • 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-29T16:11:44+00:00Added an answer on May 29, 2026 at 4:11 pm

    If you are dealing with millions of objects and stack overflows during destruction, then what you may want to look into is storing pointers to all your node objects in some type of container that doesn’t allow duplicate values and supports iteration, like a std::set or std::unordered_set. Then in the graph nodes themselves, only store pointers to the connected graph nodes. At the time of deletion for the graph, don’t recurse through each graph node’s pointers in order to delete nodes … instead, simply go to the container containing pointers to the set of nodes in the entire graph, and iterate through the container, destroying each node one-by-one. No recursion will be necessary since you won’t need to “follow” each graph-node’s set of pointers to destroy all the nodes the graph node is connected to. You also won’t need to worry about the space and time overhead of a std::shared_ptr.

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

Sidebar

Related Questions

I am using a mock object in RhinoMocks to represent a class that makes
I have a large collection of CoreData objects which represent files. Periodically, I need
We need to add a text watermark to an image, using something like imagegettftext.
I represent dates using seconds (and microseconds) since 1970 as well as a time
We're using interfaces to represent entity classes in our domain model. We have concrete
How do i represent CRLF using Hex in C#?
I often find myself using Integers to represent values in different spaces. For example...
I'm filling PDF applications using classes that represent the pdf. I have a property
Given graph, how could i represent it using adj matrix?. I have read lots
Can I represent ontology by using XML? Why it is considered better to represent

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.