当前位置: 代码迷 >> C++ >> 奇怪的内存有关问题,string or list 的有关问题
  详细解决方案

奇怪的内存有关问题,string or list 的有关问题

热度:6338   发布时间:2013-02-26 00:00:00.0
奇怪的内存问题,string or list 的问题?
#include "iostream"
#include <list>

typedef std::list<std::string*> STDLIST;
typedef std::list<std::string*>::iterator  STDLIST_ITE;

int main()
{
        STDLIST *test_list = new STDLIST();

        for (int j = 0; j < 100; ++j)
        {
                for (int i = 0; i < 20000; ++i)
                {
                        std::string     *str = new std::string("str");
                        str->reserve(i);

                        //delete str;
                        //str = NULL;
                        test_list->push_back(str);
                }

                unsigned int counter = 0;
                while (!test_list->empty())
                {
                        ++counter;
                        std::string *ptr = test_list->front();
                        delete ptr;
                        ptr = NULL;

                        test_list->pop_front();
                }
                std::cout<<"counter="<<counter<<std::endl;

                test_list->clear();

                if (j%10 == 0)
                {
                        std::cout<<"j="<<j<<",test_list->size="<<test_list->size()<<std::endl;
                        sleep(1);
  相关解决方案