Program terminated abnormally

607 views Asked by At

I'm writing a bot for codeingame.com in C++. I'm compiling my program fine on my PC. Then I'm sending it to the codeingame.com server for execution. So I have no way to debug it locally.

I've written a predicate for sorting a piece of data. Here is the relevant piece of code:

Code 1

for (const auto &zz : zone_to_zone)
{
    vector<pair<int, int>> can_go;

    for (int to : zz.second)
        can_go.push_back({ to, visit_count[to] });

    sort(can_go.begin(), can_go.end(),
        [](const pair<int, int>& z1, const pair<int, int>& z2)
    {
        return z1.second < z2.second;
    });

This program crashes on the server after number of execution of the sorting above and gives the following error:

Standard Error Stream:

Aborted.

at raise.c. function __GI_raise (sig=sig@entry=6) on line 56
at abort.c. function __GI_abort () on line 89
at libc_fatal.c. function __libc_message (do_abort=do_abort@entry=1, fmt=fmt@entry=0x7ffff6b1bc60 "*** Error in `%s': %s: 0x%s ***\n") on line 175
at malloc.c. function malloc_printerr (action=1, str=0x7ffff6b17d1e "free(): invalid pointer", ptr=<optimized out>) on line 4996
at malloc.c. function _int_free (av=<optimized out>, p=<optimized out>, have_lock=0) on line 3840
at new_allocator.h. function __gnu_cxx::new_allocator<Zone>::deallocate ( this=0x7fffffffe8e0, __p=0x61e0f0) on line 110
at alloc_traits.h. function std::allocator_traits<std::allocator<Zone> >::deallocate (__a=..., __p=0x61e0f0, __n=32) on line 383
at stl_vector.h. function std::_Vector_base<Zone, std::allocator<Zone> >::_M_deallocate (this=0x7fffffffe8e0, __p=0x61e0f0, __n=32) on line 178
at stl_vector.h. function std::_Vector_base<Zone, std::allocator<Zone> >::~_Vector_base (this=0x7fffffffe8e0, __in_chrg=<optimized out>) on line 160
at stl_vector.h. function std::vector<Zone, std::allocator<Zone> >::~vector ( this=0x7fffffffe8e0, __in_chrg=<optimized out>) on line 425

If I change the predicate slightly:

Code 2

for (const auto &zz : zone_to_zone)
{
    vector<pair<int, int>> can_go;

    for (int to : zz.second)
        can_go.push_back({ to, visit_count[to] });

    sort(can_go.begin(), can_go.end(),
        [](const pair<int, int>& z1, const pair<int, int>& z2)
    {
        return false;
    });

Then the program doesn't crash!

The server is using g++4.9. Why is this happening? What's wrong in Code 1?

0

There are 0 answers