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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:12:38+00:00 2026-05-26T07:12:38+00:00

Is it a bad idea in C++ to do something like: struct Node {

  • 0

Is it a bad idea in C++ to do something like:

struct Node
{
    string name;
    vector<Node> children;
};

I’m asking because it looks to me like resizing children for any reason could potentially cause an exponential copying cascade. But on the other hand, vector<Node*> has its own problems, and things like vector<shared_ptr<Node>>, while safe, have poor memory locality (due to too many indirections), which starts hurting when you’re trying to e.g. build a gigantic tree in memory.

So, in general, is it a bad idea to have a container of your own type in C++ (03)? Are there other reasons to avoid (or prefer) this idiom, instead of using containers of pointers?

  • 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-26T07:12:39+00:00Added an answer on May 26, 2026 at 7:12 am

    Is having a container of a type as a field a bad idea in C++?

    It’s not bad as a general rule. It really comes down to the context.

    In this case, the indirections of a shared pointer should not be significant compared to nontrivial resize operations, and the cascading (assuming that will be a problem).

    If you can reserve the proper sizes prior to populating the vector or make copying trivial, then there’s not much to worry about here.

    If it’s really gigantic, you could base your nodes of an external store, which handled (and potentially shared) the nodes. Then your resizing ops would be trivial, as your children would all be pointers.

    Depending on the size of the Nodes, this:

    struct Node
    {
        string name;
        shared_pointer<vector<Node> > children;
    };
    

    may be better than shared nodes. It really depends on the size, depth, how often you resize. A container isn’t bad, but you will have to know how your program will execute to choose the best strategy (profiling can also help here, even in cases when you figure you know the fastest).

    If you know you have a lot of objects and a lot of resizing to do, then an fast external store will be a good choice; jalf’s answer outlines a good strategy for this.

    You could also use a list of node vectors on a backing store if you will have a lot of mutations, and pointers to list elements for the children.

    Support for these more complex implementations also takes more time than your current design, but they are worth trying if you do need to perform a lot of mutations. Simplicity to implement and maintain would then be a bonus to your original design.

    If your graph really doesn’t grow very large, another approach would be to use custom allocators which either referred to a backing store of nodes, or shrank less often than the default allocators.

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

Sidebar

Related Questions

I've heard that it is a bad idea to do something like this. But
I have heard that it is a bad idea to do something like: public
I know it's generally a bad idea to do queries like this: SELECT *
Is it a bad idea to use exception chaining when throwing RemoteExceptions? We have
Is it a bad idea (and if why?) to add a a column to
Why is it a bad idea to have a ID in the URL in
Is this a bad idea? Private Class GH_DataStructureEnumerator(Of Q As Types.IGH_Goo) Implements IEnumerable(Of Q)
if this is a bad idea, how to allocate memory in the function?
Why are 'out' parameters in .NET a bad idea? I was recently asked this,
Is it a good or bad idea to make setters in java return this?

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.