I have observed stack smashing and have read the posts about it,like this Stack smashing detected. and I understood that its like we are trying to override the boundaries and gcc have prevention mechanisms for that. But in my case when I change the row index to cross the bounds its printings 0s as values but when the column index is exceeding the bounds it's causing stack smashing.
Following is the program with necessary comments
#include<stdio.h>
void main()
{
int i,j;
i=0;
j=0;
int d[2][2]={{0,0},{0,0}};
for(i=0;i<2;i++) //**when I put i<=2 there is no stack smashing**
{
for(j=0;j<2;j++) //**When I put j<=2 there comes the stack smashing**
{
if(i==j)
{
d[i][j]=1;
}
else
{
d[i][j]=0;
}
printf("%d ",d[i][j]);
}
printf("\n");
}
}
matrix$ ./a.out Output with (i<=2) and (j<2)
1 0
0 1
0 0
Out put with (i<=2) and (j<=2) matrix$ ./a.out
1 0 0
0 1 0
0 0 1
* stack smashing detected *: ./a.out terminated Aborted (core dumped)
is there any inherent limit on the number of items that can be exceeded to cause stack smashing based on architecture. Or is this just random? Any explanation would help as I couldn't figure