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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:09:31+00:00 2026-06-02T22:09:31+00:00

Consider the following code snippet: #include <vector> using namespace std; void sub(vector<int>& vec) {

  • 0

Consider the following code snippet:

#include <vector>
using namespace std;

void sub(vector<int>& vec) {
    vec.push_back(5);
}

int main() {
    vector<int> vec(4,0);
    sub(vec);
    return 0;
}

Assuming “vec” has no space left to store the 5 in the “sub” function, where does it allocate new memory?

In the stack frame of the sub function? In that case the 5 would be deleted at the end of the sub function. But the stack frame of the main function can’t grow, as the stack frame of the sub function lies on top of the stack at that moment.
Does a std::vector allocate memory for its elements on the heap?
But how does it free that heap memory?
If it’s a local vector on the stack, the stack frame of a function including the vector is deleted in the end without signaling the vector that it will be deleted?

  • 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-06-02T22:09:33+00:00Added an answer on June 2, 2026 at 10:09 pm

    Does a std::vector allocate memory for its elements on the heap?

    Yes. Or more accurately it allocates based on the allocator you pass in at construction. You didn’t specify one, so you get the default allocator. By default, this will be the heap.

    But how does it free that heap memory?

    Through its destructor when it goes out of scope. (Note that a pointer to a vector going out of scope won’t trigger the destructor). But if you had passed by value to sub you’d construct (and later destruct) a new copy. 5 would then get pushed back onto that copy, the copy would be cleaned up, and the vector in main would be untouched.

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

Sidebar

Related Questions

please consider following code #include <iostream> using namespace std; class Digit { private: int
Please let us consider following code: #include <iostream> using namespace std; union{ int i;
Consider the following code snippet: std::vector<int> v; v.reserve(100); v.insert(v.end(), 100, 5); v.erase(v.begin(), v.end()); std::cout
Consider the following code snippet: list<someClass>& method(); .... list<someClass> test = method(); What will
Consider the following short code snippet. namespace B { public class Foo { public
Consider the following code snippet: class Test { public int Length{ get; set; }
Consider the following 2 code snippets: Case 1: #include <iostream> int main() { int
Consider the following code Snippet Form form2 = new Form(); test(form2); form2.Show(); public void
Consider the following code snippet private void ProcessFile(string fullPath) { XmlTextReader rdr = new
Consider the following code snippet: void MyFun() { SetMe(); // more code if (..

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.