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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:38:39+00:00 2026-05-24T23:38:39+00:00

I know that holding pointers incurs the overhead of an extra dereference operation but

  • 0

I know that holding pointers incurs the overhead of an extra dereference operation but it saves me including the (potentially large) header file that contains the definition of my struct.

However my preference is to be determined by the advantage of having a std::vector<myStruct> *ptr2Vect member. Namely, not having to call delete on each element. How big a performance advantage is this? Can vector really allocate objects on the stack? I am fairly new to template classes and wonder if it could be possible for a dynamic array to expand on the stack and at what price?

_EDIT_

I fail in understanding default copy constructor and operator= members and am trying to keep things as simplistic structs. I have neither implementation defined explicitly so fear that making the vector element an object instead of pointer will create temporary object at assignment time that will be destructed and so ruin its copy.

_EDIT_

Sorry for the delay in delivering pertinent information (I am shy with my code).

I want to call push_back(newObj). Now if I don’t use pointers I have a big problem in that I don’t want to perform a deep copy but my dtor will free up the memory shared by the LHS and RHS of this invocation of the copy constructor.

  • 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-24T23:38:40+00:00Added an answer on May 24, 2026 at 11:38 pm

    As a general rule of thumb I’d say you probably don’t want to put pointers in your containers, unless there’s a good reason.

    Possible reasons to consider pointers:

    • You have virtual functions
    • You have a class hierarchy
    • You don’t know the size of the objects where you’re using them this. (You can only use pointers or references in that case and you can’t have a vector of references)
    • Your objects are exceedingly large (probably benchmark this)

    The biggest reason not to put pointers in containers would be that it makes it much easier not to make a mistake and accidentally leak memory. This is especially true when you start to consider exceptions.

    Not having pointers in your containers makes it much easier to use STL <algorithms>, consider:

    #include <vector>
    #include <string>
    #include <iostream>
    #include <iterator>
    #include <algorithm>
    
    int main() {
      std::vector<std::string> test;
      test.push_back("hello world");
      std::copy(test.begin(), test.end(), 
                std::ostream_iterator<std::string>(std::cout, "\n"));
    }
    

    Versus:

    #include <vector>
    #include <string>
    #include <iostream>
    #include <iterator>
    #include <algorithm>
    
    int main() {
      std::vector<std::string*> test;
      // if push_back throws then this will leak:
      test.push_back(new std::string("hello world"));
      // Can't do:
      std::copy(test.begin(), test.end(), 
                std::ostream_iterator<std::string>(std::cout, "\n"));
      // Will now leak too
    }
    

    (which I would never do)

    Or possibly:

    #include <vector>
    #include <string>
    #include <iostream>
    #include <iterator>
    #include <algorithm>
    
    int main() {
      std::vector<std::string*> test;
      std::string str("hello world");
      test.push_back(&str);
      // Can't do:
      std::copy(test.begin(), test.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
    }
    

    But the semantics of this one make me feel uncomfortable – it’s not clear at all that delete elsewhere in the code would be a very bad thing and you still can’t use STL algorithms very comfortably even if there is no leak issue.

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

Sidebar

Related Questions

I know that it's a subject that can raise a lot of debate, but
I know that I should put all the html elements in body tag, but
I know that this has already been asked here but the answer (using a
I know that one of the differences between classes and structs is that struct
I know that exceptions have a performance penalty, and that it's generally more efficient
We know that behind the scenes, the ASP.NET MVC framework will use reflection to
I know that one can define an 'expected' exception in JUnit, doing: @Test(expect=MyException.class) public
I know that it's possible to implicitly provide asynchronous interaction using: Asynchronous Delegates Asynchronous
I know that Powershell is quite powerful in that it is a scripting language
I know that it's OK to install VS 2010 B1 on a system with

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.