I have confirmed x[i] exist for all i in [0,n) through debugger.
Though I am using a vector of lists in the code, I think the problem purley orginates from std::list.push_back(). As far as I know, std::list.push_back() should always succeed only if memory can be allocated for the new node.
Tracing the memory error reveals that the error occurs from "allocate" down in the function called by push_back, as you can see below.
Code:
Invalid read of size 4
at 0x1B990642: std::__default_alloc_template<true, 0>::allocate(unsigned) (in /usr/lib/libstdc++.so.5.0.7)
by 0x8050890: std::__simple_alloc<std::_List_node<int>, std::__default_alloc_template<true, 0> >::allocate(unsigned) (stl_alloc.h:232)
by 0x8050863: std::_List_alloc_base<int, std::allocator<int>, true>::_M_get_node() (stl_list.h:277)
by 0x80517B2: std::list<int, std::allocator<int> >::_M_create_node(int const&) (stl_list.h:413)
by 0x8050A3F: std::list<int, std::allocator<int> >::insert(std::_List_iterator<int, int&, int*>, int const&) (list.tcc:89)
by 0x804F791: std::list<int, std::allocator<int> >::push_back(int const&) (stl_list.h:748)
Is this a memory leak?