The following code works correctly, but it throws run-time error when I run it in the Leetcode Playground
#include <bits/stdc++.h>
using namespace std;
void print2d(vector<vector<int>> v) {
for (int r = 0; r < v.size(); r++) {
for (int c = 0; c < v[0].size(); c++) {
cout << v[r][c] << " ";
}
cout << endl;
}
}
int main() {
vector<vector<int>> res {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
res[0].push_back(10);
print2d(res);
}Here is the error
1 2 3 10
=================================================================
==30==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6020000000fc at pc 0x0000003783ce bp 0x7ffed76d8410 sp 0x7ffed76d8408
READ of size 4 at 0x6020000000fc thread T0
#2 0x7fe450bfc0b2 (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
0x6020000000fc is located 0 bytes to the right of 12-byte region [0x6020000000f0,0x6020000000fc)
allocated by thread T0 here:
#13 0x7fe450bfc0b2 (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
Shadow bytes around the buggy address:
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 fd fd fa fa fd fd fa fa fd fd
=>0x0c047fff8010: fa fa 00 04 fa fa 00 04 fa fa 00 00 fa fa 00[04]
0x0c047fff8020: fa fa 00 04 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
0x0c047fff8060: 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