Скоро Codeforces Round 1133 (Div. 1, Div. 2)! Регистрация здесь. ×

Блог пользователя AQZZ

Автор AQZZ, история, 6 лет назад, По-английски

Currently I compile with these flags: alias bd='g++ -std=c++17 -Wshadow -Wall -Wextra -o "a" -g -fsanitize=address -fsanitize=undefined'

Most of the time when I access an out of bounds index I get an error from the sanitizer that tells me the line number, which is good. But sometimes I just get an error that memory is leaked but I get no line number. Are there any flags/compilers which can print the line number consistently when this error occurs, similar to Java's index out of bounds exception?

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Use std::vector::at() instead of std::vector::operator []() to get an exception on accessing elements out of range.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Add -D_GLIBCXX_DEBUG (slows your program, but does comprehensive checks) or -D_GLIBCXX_ASSERTIONS (faster, but less error checking) to your compiler flags. These flags check for out of bounds indexing. Then you can run your code in a debugger like gdb, and the debugger will stop the execution of the program when the program aborts on out of bounds indexing.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

You can use the __LINE__ C preprocessor macro to write your own bounds-checking function that prints the line number at which the out-of-range error takes place. The following is an example.

Example

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

If you look at https://github.com/google/sanitizers/wiki/AddressSanitizer, you would get:

To get nicer stack traces in error messages add -fno-omit-frame-pointer

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Where can I find the line number?

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    If you use the D_GLIBCXX_DEBUG flag, then when you use gdb and type "run" and get to the error you can type "where" to see the line number. If you leave out the D_GLIBCXX_DEBUG flag, most of the time you get the line number at runtime from the sanitizers, and when you don't you will get a vague memory leak error, but using gdb you can find the line number just as before. Using the D_GLIBCXX_DEBUG flag, you don't get any line numbers at runtime, but the error description is more precise.