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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:43:13+00:00 2026-05-28T02:43:13+00:00

I came across the web (don’t recall where) a note that says that MS

  • 0

I came across the web (don’t recall where) a note that says that MS c++ stl containers have a memory leak in their clear() API.

Therefore if you have a:

void main()
{ 
   std::vector<int> vVec;
   for(int i =0; i < 100; i++)
      vVec.push_back(i);

   vVec.clear();
}

Therefore the memory allocated on the heap for the vector is not really released…

The note said (as far as i recall) the following technique to make sure the memory is really released…

void main()
{ 
   std::vector<int> vVec;
   for(int i =0; i < 100; i++)
      vVec.push_back(i);

   vVec.clear();
   vector<int>(vVec).swap(vVec);
}

Do you have experience with such? is the above true? and if yes, what actually happening here?

(and last question, sorry, is this true for all other stl containers?)
Thanks,

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

    It’s not a memory leak; it’s the required behavior (by the standard).
    std::vector<>::clear() is not allowed to lower the capacity, and thus
    cannot free its buffer. The memory will be freed when the
    destructor is called, and in swap, the buffers will be swapped, so in

    vector<int>().swap(vVec);
    

    , the temporary object gives vVec its (empty) buffer, and receives the
    non-empty buffer of vVec, which it deletes at the end of the full
    expression.

    This is normally not needed, or even wanted, after clear; if you want
    a completely new vector, just declare one. On the other hand, if you’ve
    been filling a vector gradually, it could easily have a capacity of more
    than is needed, and an excessively large buffer. In this case:

    vector<int>(vVec).swap(vVec);
    

    will first make a (temporary) copy with an exactly sized buffer, then
    swap buffers with vVec. The results will be that vVec has a
    capacity equal to its size, and no more. (Formally, this isn’t
    guaranteed by the standard anywhere, but in practice, it corresponds to
    all of the implementations I know of.)

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

Sidebar

Related Questions

I recently came across a web page using the font ff-tisa-web-pro-1 (specified in their
I came across these on the new job I just started. I don't have
I recently came across a ASP 1.1 web application that put a whole heap
So I came across an interesting problem today. We have a WCF web service
While passing through code in our project I came across a web method that
I came across the Dell Studio One 19 on the web the other day,
In searching the web I came across http://groupaware.mobi/iphone which has a sample iphone navigation
So I'm working on this VB to C# web application migration and came across
While viewing the source of a web page, I came across this CSS, applied
I recently came across a html email that contained _label attributes on the <

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.