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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:52:00+00:00 2026-05-18T02:52:00+00:00

Whenever I need to add dynamically allocated object into a vector I’ve been doing

  • 0

Whenever I need to add dynamically allocated object into a vector I’ve been doing that the following way:

class Foo { ... };

vector<Foo*> v;

v.push_back(new Foo);

// do stuff with Foo in v

// delete all Foo in v

It just worked and many others seem to do the same thing.

Today, I learned vector::push_back can throw an exception. That means the code above is not exception safe. 🙁 So I came up with a solution:

class Foo { ... };

vector<Foo*> v;
auto_ptr<Foo> p(new Foo);

v.push_back(p.get());
p.release();

// do stuff with Foo in v

// delete all Foo in v

But the problem is that the new way is verbose, tedious, and I see nobody’s doing it. (At least not around me…)

Should I go with the new way?
Or, can I just stick with the old way?
Or, is there a better way of doing it?

  • 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-18T02:52:00+00:00Added an answer on May 18, 2026 at 2:52 am

    If all you care about is exception-safety of this operation:

    v.reserve(v.size()+1);  // reserve can throw, but that doesn't matter
    v.push_back(new Foo);   // new can throw, that doesn't matter either.
    

    The issue of a vector having responsibility for freeing the objects pointed to by its contents is a separate thing, I’m sure you’ll get plenty of advice about that 😉

    Edit: hmm, I was going to quote the standard, but I actually can’t find the necessary guarantee. What I’m looking for is that push_back will not throw unless either (a) it has to reallocate (which we know it won’t because of the capacity), or (b) a constructor of T throws (which we know it won’t since T is a pointer type). Sounds reasonable, but reasonable != guaranteed.

    So, unless there’s a beneficial answer over on this question:

    Is std::vector::push_back permitted to throw for any reason other than failed reallocation or construction?

    this code depends on the implementation not doing anything too “imaginative”. Failing that, your solution from the question can be templated up:

    template <typename T, typename Container>
    void push_back_new(Container &c) {
        auto_ptr<T> p(new T);
        c.push_back(p.get());
        p.release();
    }
    

    Usage then isn’t too tedious:

    struct Bar : Foo { };
    
    vector<Foo*> v;
    push_back_new<Foo>(v);
    push_back_new<Bar>(v);
    

    If it’s really a factory function rather than new then you could modify the template accordingly. Passing a lot of different parameter lists in different situations would be difficult, though.

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

Sidebar

Related Questions

I've been using this pattern whenever I need to create a class that might
I'm working on a form where I need to dynamically add inputs whenever the
I have a user control UserApplianceControl that I need to dynamically add to a
I making a custom button, and I need to add a PreviewKeyDown event, whenever
I need to notify my application in some way whenever any file in the
I need to write a trigger that rounds a value whenever it is inserted/updated
Java Code : DOMConfigurator.configureAndWatch(log4jConfigFile.getAbsolutePath(),100000L); I need to add some appenders programmaticaly whenever Thread created
I have an unordered list which is dynamically created, and I need to add
I created a table, on referring http://stackoverflow.com/questions/9045940/dynamically-adding-table-row , whenever i came into the last
I've got a third-party component that does PDF file manipulation. Whenever I need to

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.