简体   繁体   中英

std::bad_alloc on declaration of a list c++

I'm trying to play around with lists and create a list of lists that contain integers. After I run the code it thorws a bad alloc exception:

"aterminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc

This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information."

#include <iostream>
#include <list>
#include <string>

int main() {
    std::list<int> l{1,2,3,4};//
    std::list<int> l1{5,6,7,8};
    std::list<std::list<int>> ll;// list of lists<int>
    std::cout << "a"; //does not reach here, terminated before this row
    ll.push_back(l);
    ll.push_back(l1);
    std::list<std::list<int>>::iterator itr;
    for (itr=ll.begin(); itr != ll.end(); itr++)
    {
        std::list<int>tl=*itr;
        std::list<int>::iterator it;
        for (it=tl.begin(); it != tl.end(); it++)
        {
            std::cout<<*it;
        }
        std::cout<<std::endl<<"End"<<std::endl;
    }
    return 0;
}

I'm running it in Clion on Windows 10 with minGW. How can I fix this? everything seems right.

I compiled you code inside of Visual Studio 2015. Your code compiled with no errors and the output was as follows.

a1234

End

5678

End

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM