help needed in debug the error 'std::out_of_range' what(): vector::_M_range_check

Dear All

While running my code, I am getting error

"terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check"

I am wanted to know at which part of program this error is happening.

I tried gdb , but it is giving this message from which i am unable to trace the error location

"
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check

Program received signal SIGABRT, Aborted.
0x0012d422 in __kernel_vsyscall ()
"

I also tried valgring, but it stuck after running few lines. it gives this message and then stop

$ valgrind --leak-check=full --show-reachable=yes ./icp
==2222== Memcheck, a memory error detector
==2222== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==2222== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
==2222== Command: ./icp
==2222==
check 0
x size= 9
y size= 9
angle size= 3



is there any other way to find out this problem?

irshan
Last edited on
Compile the program with -g and then use bt in gdb

Here's an example:

1
2
3
4
5
6
7
8
9
10
#include <vector>
int bad_function()
{
        std::vector<int> v;
        return v.at(20);
}
int main()
{
        bad_function();
}


After compiling that and running in gdb, I see

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
(gdb) run
Starting program: /home/cubbi/test
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check

Program received signal SIGABRT, Aborted.
0x00007ffff72eaa45 in raise () from /lib64/libc.so.6
(gdb) bt
#0  0x00007ffff72eaa45 in raise () from /lib64/libc.so.6
#1  0x00007ffff72ebebb in abort () from /lib64/libc.so.6
#2  0x00007ffff7b91e4d in __gnu_cxx::__verbose_terminate_handler() ()
   from /usr/lib/gcc/x86_64-pc-linux-gnu/4.6.2/libstdc++.so.6
#3  0x00007ffff7b8fff6 in __cxxabiv1::__terminate(void (*)()) ()
   from /usr/lib/gcc/x86_64-pc-linux-gnu/4.6.2/libstdc++.so.6
#4  0x00007ffff7b90023 in std::terminate() ()
   from /usr/lib/gcc/x86_64-pc-linux-gnu/4.6.2/libstdc++.so.6
#5  0x00007ffff7b9011e in __cxa_throw ()
   from /usr/lib/gcc/x86_64-pc-linux-gnu/4.6.2/libstdc++.so.6
#6  0x00007ffff7b3ddf7 in std::__throw_out_of_range(char const*) ()
   from /usr/lib/gcc/x86_64-pc-linux-gnu/4.6.2/libstdc++.so.6
#7  0x0000000000400951 in std::vector<int, std::allocator<int> >::_M_range_check (this=0x7fffffffd650, __n=20)
    at /usr/lib/gcc/x86_64-pc-linux-gnu/4.6.2/include/g++-v4/bits/stl_vector.h:719
#8  0x000000000040082d in std::vector<int, std::allocator<int> >::at (
    this=0x7fffffffd650, __n=20)
    at /usr/lib/gcc/x86_64-pc-linux-gnu/4.6.2/include/g++-v4/bits/stl_vector.h:737
#9  0x000000000040074a in bad_function () at test.cc:5
#10 0x0000000000400784 in main () at test.cc:9
(gdb)


In my case, this says that the error was due to passing the value 20 (__n=20) to vector::at() on line 5 of file test.cc in the function bad_function()


Thanks for the such a brief and good explanation :), it solved my problem.
Topic archived. No new replies allowed.