how to get what line makes error if I run program in terminal

how to get what line makes error if I run program in terminal
I might know where It can be generated, but It will be helpful, if can get line number for the error

terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::substr: __pos (which is 2) > this->size() (which is 0)
Press <RETURN> to close this window...
Use a debugger. For example you can locate the error in this program:

1
2
3
4
5
6
7
8
9
10
#include <string>
#include <iostream>

int main()
{
  std::string a;
  std::string b = a.substr(2);

  std::cout << b << '\n';
}


Using GDB like this:
mbozzi@QZPD:~$ g++ -Wall -Wextra -pedantic-errors -ggdb -O0 a.cpp
mbozzi@QZPD:~$ ./a.out
terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::substr: __pos (which is 2) > this->size() (which is 0)
Aborted
mbozzi@QZPD:~$ gdb -q ./a.out
Reading symbols from ./a.out...
(gdb) run
Starting program: /home/mbozzi/a.out
terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::substr: __pos (which is 2) > this->size() (which is 0)

Program received signal SIGABRT, Aborted.
__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
50      ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
#1  0x00007ffff7bee859 in __GI_abort () at abort.c:79
#2  0x00007ffff7e77911 in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6
#3  0x00007ffff7e8338c in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6
#4  0x00007ffff7e833f7 in std::terminate() () from /lib/x86_64-linux-gnu/libstdc++.so.6
#5  0x00007ffff7e836a9 in __cxa_throw () from /lib/x86_64-linux-gnu/libstdc++.so.6
#6  0x00007ffff7e7a3ab in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6
#7  0x00007ffff7f1ef48 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::substr(unsigned long, unsigned long) const () from /lib/x86_64-linux-gnu/libstdc++.so.6
#8  0x000055555555528d in main () at a.cpp:7
(gdb) frame 8
#8  0x000055555555528d in main () at a.cpp:7
7         std::string b = a.substr(2);

Hello. I don't know which compiler you are using - or which OS is running on your PC. I use Visual Studio on Win10. In my case, if compilation has been possible, there is no way to know which line in my code had generated an error. As mbozzi says, you should use a debugger to trace processes. However you can integrate all along processes some debug info in order to go through until you reach a fatal error. I used this way often when I developed games on Unity to catch bugs with Debug.Log("something goes wrong here!");
Take a look at this macro which can be useful. It allows to display a large information about file, function and line in which it has been called. This way you can trace each process until something happens killing the program ++

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
29
30
31
32
33
34
35
#include <iostream>

#ifndef DEBUG 
#define DEBUG 1
#endif

#if DEBUG
int cc = 1;

#define log(...) {\
    char str[100];\
    sprintf_s(str, __VA_ARGS__);\
    std::cout << "[" << __FILE__ << "][" << __FUNCTION__ << "][Line " << __LINE__ << "] " << str << " " << cc << std::endl;\
    ++cc;\
}
#else
#define log(...)
#endif

int foo(int x)
{
    std::cout << "Here!\n";
    log("> break point");
    return x;
}

int main()
{
    std::cout << "Hello World!\n";
    log("> break point");

    foo(42);

    return 0;
}



Hello World!
[C:\Users\sicch\Desktop\DebugLog\DebugLog\DebugLog.cpp][main][Line 30] > break point 1
Here!
[C:\Users\sicch\Desktop\DebugLog\DebugLog\DebugLog.cpp][foo][Line 23] > break point 2
Last edited on
In the same time, maybe you would like to simplify the Debug log process using instead a simple function. This alternative works well as expected. Fundamentally the same code than the previous, but stripped down ++ :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define HERE __FILE__ " " __FUNCTION__ " : LINE " TOSTRING(__LINE__)

void breakPoint(const char* loc)
{
    std::cout << loc << std::endl;
}

int foo(int x)
{
    breakPoint(HERE);
    return x;
}

int main(int)
{
    breakPoint(HERE);
    foo(42);

    return 0;
}



C:\Users\sicch\Desktop\DebugLog\DebugLog\DebugLog.cpp main : LINE 20
C:\Users\sicch\Desktop\DebugLog\DebugLog\DebugLog.cpp foo : LINE 14

https://www.decompile.com/cpp/faq/file_and_line_error_string.htm
Last edited on
C++20:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
#include <source_location> // https://en.cppreference.com/w/cpp/utility/source_location
#include <filesystem>

std::ostream& dump_location( std::source_location loc = std::source_location::current() )
{
    return std::cout << std::filesystem::path( loc.file_name() ).filename() << ", function: "
                     << loc.function_name() << ", line: " << loc.line() << '\n' ;
}

int foo( int x ) { dump_location() ; return x ; }

int main()
{
    dump_location() ;
    foo(42) ;
}

http://coliru.stacked-crooked.com/a/53e56aad7ede81b1
@JLBorges - really neat solution. Adopted ++
Topic archived. No new replies allowed.