Why am I getting this heap-buffer-overflow error? The code works in Visual Studio.
class Solution {
public:
// T_0 = 0
// T_1 = 1
// T_2 = 1
// T_3 = 2 (0 + 1 + 1)
// T_4 = 4 (1 + 1 + 2)
int tribonacci(int n) {
std::vector<int> seq(n + 1, 0);
seq[0] = 0;
seq[1] = 1;
seq[2] = 1;
for (int i = 3; i <= n; i++) {
seq[i] = seq[i-1] + seq[i-2] + seq[i-3];
}
return seq[n];
}
};=================================================================
==30==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000000034 at pc 0x000000343d26 bp 0x7ffc926cffd0 sp 0x7ffc926cffc8
WRITE of size 4 at 0x602000000034 thread T0
#2 0x7f6db965d0b2 (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
0x602000000034 is located 0 bytes to the right of 4-byte region [0x602000000030,0x602000000034)
allocated by thread T0 here:
#5 0x7f6db965d0b2 (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
Shadow bytes around the buggy address:
0x0c047fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c047fff8000: fa fa fd fd fa fa[04]fa fa fa fa fa fa fa fa fa
0x0c047fff8010: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
Shadow gap: cc
==30==ABORTING