My array is of size 3x3 means i have values of index from 0 to 2 only. but when i traverse using for loop then why it is picking value of a[3][-3] on value of a[2][0] ???
and what is that error when i try a[3][3] it should give garbage value so why this error
*** stack smashing detected ***: terminated
#include <bits/stdc++.h>
#include <iostream>
#define vi vector<int>
using namespace std;
int main()
{
char a[3][3];
int pos, row, col;
cin >> pos;
memset(a, 48, sizeof(a));
//row = pos / 3;
//col = pos % 3 - 1;
a[3][3] = 'X';
//a[3][-3] = 'X';
for (char *b : a)
{
for (size_t i = 0; i < 3; i++)
{
cout << b[i] << " ";
}
cout << endl;
}
}
for a[3][-3] result will be output:
0 0 0
0 0 0
X 0 0
for a[3][3] result will be output:
0 0 0
0 0 0
0 0 0
*** stack smashing detected ***: terminated
Aborted (core dumped)
This is because of wrong arithmetic as @Kevin answer in comment that
a[2][0] = 2*3 + 0 = 6
bytes
and also
a[3][-3] = 3*3 - 3 = 6
bytes
so both pointing to same value.