0

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.

  • 5
    [Works here](http://coliru.stacked-crooked.com/a/e6eabec99e96e2e7) – NathanOliver Jan 06 '17 at 18:29
  • 1
    This works with Visual Studio 2015. – François Andrieux Jan 06 '17 at 18:31
  • I suspect this is related to http://stackoverflow.com/questions/20621639/stdendl-crashes-windows-8-compiled-using-mingw or http://stackoverflow.com/questions/11975941/simple-program-crashes where the standard library `cout`/`endl` had some sort of library ABI mismatch. – Mark B Jan 06 '17 at 18:40
  • 1
    If you use tracing output for debugging, you need to flush `cout` every time, or write to `cerr`. – molbdnilo Jan 06 '17 at 18:41

1 Answers1

0

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