Cpp: Errors and Exceptions

Oct. 21, 2022, edited on Mar. 22, 2023

The Common types of Errors

Out of Bounds Errors

vector<int> v(5);
int x = v[5]; // out of bounds
cout << x << endl;

Now x is out of bounds of array, but there is no tips when building and running.

g++ ./a.cpp && ./a.out

When we compile and run this code with AddressSanitizer enabled, we get the following output:

# g++ -fsanitize=address -fno-omit-frame-pointer -g ./a.cpp && ./a.out 
=================================================================
==557143==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x603000000054 at pc 0x000000401676 bp 0x7ffcec886380 sp 0x7ffcec886378
READ of size 4 at 0x603000000054 thread T0
    #0 0x401675 in main a.cpp:10
    #1 0x7f5629f0824d in __libc_start_call_main (/nix/store/c6f52mvbv0d8rd3rlslsvy7v4g3pmm7p-glibc-2.35-163/lib/libc.so.6+0x2924d)
    #2 0x7f5629f08308 in __libc_start_main_impl (/nix/store/c6f52mvbv0d8rd3rlslsvy7v4g3pmm7p-glibc-2.35-163/lib/libc.so.6+0x29308)
    #3 0x4017d4 in _start (/home/luo/code/cpp-exercise/a.out+0x4017d4)

SUMMARY: AddressSanitizer: heap-buffer-overflow a.cpp:10 in main