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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:29:24+00:00 2026-05-25T11:29:24+00:00

What is the proper way of pushing a new object element onto a std::vector

  • 0

What is the proper way of pushing a new object element onto a std::vector? I want the data to be allocated in the vector. Will this copy the object newradio into the vector and then get rid of newradio when it goes out of scope (e.g. out of the stack)?

vector<Radio> m_radios;
Radio newradio(radioNum);
m_radios.push_back(newradio);

And then when I free the object containing m_radios, will this free all the memory allocated by the vector?

  • 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-25T11:29:25+00:00Added an answer on May 25, 2026 at 11:29 am

    std::vector manages its own memory. That means that, when the destructor of a vector is invoked the memory held by the vector is released. std::vector also invokes an object’s destructor when it is removed (through erase, pop_back, clear or the vector’s destructor).

    When you do this:

    Radio newradio(radioNum);
    m_radios.push_back(newradio);
    

    You add a copy of newradio (created using Radio‘s copy constructor) to the vector. newradio will be destroyed when it goes out of scope, and the copy will be destroyed when it is removed from the vector (as for any object).

    That’s an important point: std::vector only stores copies of an object, which means the object must have a meaningful copy constructor (and an assignment operator, but that’s another issue). If you have a vector of pointers, then the pointer itself will be copied, and not what it points to. Note that this behavior is the same for every standard container (like std::list or std::set).

    As a rule of thumb, if you’re not using pointers, then you don’t have to worry about releasing the memory yourself.

    In C++11, most standard containers (including vector) have an emplace_back method that constructs an object in place at the end of the container. It takes a bunch of parameters and calls the constructor that best matches those parameters (or fails if no such constructor exist), using said constructor to create the object, without any copy, at the end of the container. So, the above code could be rewritten as:

    m_radios.emplace_back(radioNum); // construct a Radio in place, 
                                     // passing radioNum as the constructor argument
    

    Also in C++11, containers are usually move aware, so they no longer require objects to be copiable: if they are movable, then the container will move its contents as required (such as during reallocations, for instance). Copiable types are still required if you want to copy the vector.

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

Sidebar

Related Questions

I'm relatively new to JavaScript and I've been having trouble pushing data into an
What's the proper way to create an object (with its namespaces and such)? 1
The proper way to end a vb6 program is to do something like this
Is this a proper way to declare immutable structs? public struct Pair { public
Is there a proper way to do this: $i = 0; while($row = mysql_fetch_array($queryResult)){
What is the proper way to document objects of this style in jsdoc: /**
What's the proper way to initialize a class_attribute in Rails? I am using this:
What is the most proper way to sending email of minimal 1000 or more
What's the proper way to convert from a scientific notation string such as 1.234567E-06
What is the proper way to minimize a WinForms app to the system tray?

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.