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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:00:41+00:00 2026-05-27T01:00:41+00:00

Suppose I have complex objects, like structs containing a huge amount of members –

  • 0

Suppose I have complex objects, like structs containing a huge amount of members – and I want to store them in a vector.

Now vector::push_back(…) works over call-by-reference (rather than call-by-value), so in the first moment the passed object is not copied. But what about later? Does a vector internally store pointers or direct references? When the vector needs to expand, are the contained elements themselves copied or their addresses?

This finally results in the question, if – for large objects – the objects themselves should be stored in a vector or rather pointers to these objects. Is there a kind of best practice for that?

  • 1 1 Answer
  • 3 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-27T01:00:41+00:00Added an answer on May 27, 2026 at 1:00 am

    std::vector::push_back saves copy of the object.

    If you need performance by avoiding copy, then you could use vector of pointers.

    std::vector<BigObject*> objects; //manage memory yourself
    

    Or you can use some sort of smart pointer, so as to avoid managing memory yourself.
    For example, you could use std::unique_ptr if your compiler supports it:

    std::vector<std::unique_ptr<BigObject>> objects;
    

    If you use pointers (or smart pointers), then copying is still there, but this time, the copy is done for pointers (or smart pointers) which is much cheaper.

    From @Loki’s comment:

    Or you can use boost::ptr_vector<> which is designed to hold pointers and will return references when accesses thus makeing it easy to use standard algorithms as they all expect objects.

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

Sidebar

Related Questions

Suppose you have a list of 100 complex nested object, and you want to
Suppose I have a class like so: public class StaticDude{ public static Object getGroove()
Suppose I have a complex system where there large trees of people. Simple thoughts
Suppose I have a class like this: public class ThingManager { List<SomeClass> ItemList; public
Suppose I have a simple interface representing a complex number, whose instances would be
I have an object with has multiple similar complex properties. I would like to
i have complex XML structure i want get all descendents uisng some xml paren.child.@attribute
I have a complex heirarchy of JSON objects posting to a WCF service. By
I have a set of pretty complex forms that I'd like to implement in
Suppose I have an object foo in my JavaScript code. foo is a complex

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.