Search code examples
c++11coredumpbucket-sort

core dump stack indicates SIGSEGV due to vector<vector<int>> usage


I have a code snippet that is behaving weirdly. The code is simply aiming to implement radix and bucket sort. When I comment in the main one of either sort and run it works perfectly. But when I enable both of them i am getting a core dump. And the weird part is core dump as indicated by the stack is crossing over into the stl_vector.h.

The code reference is here:- https://rextester.com/RUUDP10453

When i enable only one of the sorts like below in main it works fine.

  //doRadixSort(arr, size);
    doBucketSort(arr, size);
   or
  doRadixSort(arr, size);
  //doBucketSort(arr, size);  

But when both are enabled there is segmentation fault after both sorts are completed as indicated by the

cout << "i am here at exit" << endl;

The core dump stack indicates some reference/hint at vector of vector buckets. But i have properly allocated and reserved it the required memory. so why this is happening i need some expertise to dig out. I have tried debugging this in eclipse CDT C++ for about 2 hrs with no lead.

Program terminated with signal SIGSEGV, Segmentation fault.
#0  _int_free (av=0x7f66d702eb00 <main_arena>, p=0xf98020, have_lock=0) at malloc.c:3976
3976                  >= ((char *) av->top + chunksize(av->top)), 0))
(gdb) where
#0  _int_free (av=0x7f66d702eb00 <main_arena>, p=0xf98020, have_lock=0) at malloc.c:3976
#1  0x00007f66d6cf33dc in __GI___libc_free (mem=<optimized out>) at malloc.c:2966
#2  0x00000000004030fa in __gnu_cxx::new_allocator<std::vector<int, std::allocator<int> > >::deallocate (this=0x7fffc6ffa060, __p=0xf98030) at /usr/include/c++/6.3.1/ext/new_allocator.h:110
#3  0x0000000000402d23 in std::allocator_traits<std::allocator<std::vector<int, std::allocator<int> > > >::deallocate (__a=..., __p=0xf98030, __n=10) at /usr/include/c++/6.3.1/bits/alloc_traits.h:442
#4  0x00000000004027ac in std::_Vector_base<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >::_M_deallocate (this=0x7fffc6ffa060, __p=0xf98030, __n=10)
    at /usr/include/c++/6.3.1/bits/stl_vector.h:178
#5  0x00000000004025e4 in std::_Vector_base<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >::~_Vector_base (this=0x7fffc6ffa060, __in_chrg=<optimized out>)
    at /usr/include/c++/6.3.1/bits/stl_vector.h:160
#6  0x000000000040211d in std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >::~vector (this=0x7fffc6ffa060, __in_chrg=<optimized out>)
    at /usr/include/c++/6.3.1/bits/stl_vector.h:427
#7  0x0000000000401d4b in doBucketSort (arr=0x7fffc6ffa100, size=@0x7fffc6ffa0f8: 12) at tako.cpp:97
#8  0x0000000000401e29 in main (argc=1, argv=0x7fffc6ffa218) at tako.cpp:141
(gdb) 

Solution

  • Alternatively, I found the below also works which is equivalent to the resize function.

    vector<vector<int>> buckets; 
    constexpr size_t size=10, bucketSize=10;
    buckets.reserve(bucketSize); 
    for(unsigned int i=0; i<=bucketSize; ++i) 
        buckets.push_back({ });  
    for(unsigned int i=0; i<=bucketSize; ++i) 
        buckets[i].reserve(size);