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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:25:31+00:00 2026-06-05T04:25:31+00:00

I have following problem: When I use std::vector with built-ins i don’t get a

  • 0

I have following problem:

When I use std::vector with built-ins i don’t get a memory but if I use classes I get memory leak. To illustrate:

//No leak
std::vector<double>* vecPtr1=new std::vector<double>();
//add some elements
delete vecPtr1;

//Leaks some memory but not all
std::vector<SomeClass>* vecPtr2=new std::vector<SomeClass>();
//add some elements with vecPtr2->push_back(SomeClass());
delete vecPtr2;

As far as I understand it delete should call the destructor of std::vector which should in turn call the destructor of SomeClass -> no leak. I have invested some thought and testing into this and the same behaviour happens if I use std::vector in a scope such as here:

{
  std::vector<SomeClass> vector;
  //add elements as before
}
//memory is still used here

I’m using gcc 4.6.1 under Ubuntu 11.10. Is something amiss in my library or do I have a misconception how std::vector destructs elements?

For clarification my complete code with SomeClass replaced with std::pair (yes I know some parts are hacked but it’s just an example):

#include <iostream>
#include <vector>
#include <utility>

int main()
{
    std::string inString;
    std::cout<<"Started"<<std::endl;
    //wait
    std::cin>>inString;
    {
        //assign vector
        std::vector<std::pair<std::string,unsigned int> > vec=std::vector<std::pair<std::string,unsigned int> >();
        //push elements
        for(unsigned int i=0;i<1e7;++i)
        {
            vec.push_back(std::pair<std::string,unsigned int>("something",i));
        }
        std::cout<<"Created vector with capacity: "<<vec.capacity()<<std::endl;
        //wait
        std::cin>>inString;
    }
    //vec should go out of scope but not all memory gets freed
    std::cout<<"Deleted vector"<<std::endl;
    //wait
    std::cin>>inString;
    std::cout<<"Shutting down"<<std::endl;

    return 0;
}
  • 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-05T04:25:33+00:00Added an answer on June 5, 2026 at 4:25 am

    First of all, you should not create object of vector dynamically. That is simply a bad idea. That is, use automatic vector:

    std::vector<SomeClass> classes; //automatic object
    

    Anyway, in your case, I guess the problem lies with the class SomeClass.

    I guess this class manages memory and has not implemented at least one of the following properly:

    • Copy-constructor
    • Copy-assignment
    • Destructor

    If you’re using C++11, then there are two more members:

    • Move-constructor
    • Move-assignment

    I would suggest you to read these:

    • What is The Rule of Three?
    • Rule-of-Three becomes Rule-of-Five with C++11?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to use JSTL but have the following problem: Index.xhtml page: <?xml version=1.0 encoding=UTF-8?>
I have the following problem: I need to use XSLFO to generate a 2-column
I have following problem: I have built a tabbar application with 4 tabs. I
I have the following problem. I have three classes, A, B and C. A
Suppose I have the following two data structures: std::vector<int> all_items; std::set<int> bad_items; The all_items
I'm trying to use STL to solve the following problem (I don't want to
A different question inspired the following thought: Does std::vector<T> have to move all the
I have a big problem with std::wstring memory allocation. The program crash when I
I'm using VS2008 (so I have TR1, but no C++11), and I don't use
Please help me with the following problem: I have the following classes: class ChemicalElement

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.