Home > Software design >  Stack smashing detected. What it means and how can I fix it?
Stack smashing detected. What it means and how can I fix it?

Time:12-13

The only elements in the array that I want are the ones that im transforming into 1, but for some reason the when im navigating trough the lower part this stack smashing occur.

```
#include <stdio.h>
#define tam 12

int main(void) {
  int i, j;
  int matriz[tam][tam]={{0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0}, 
                        {0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0}};
  int controle = 0, controle2 = 0;

  for(i = 0; i<6;i  ){
    for(j=i;j<tam-i;j  ){
      matriz[tam-(i)][tam-(j 1)]=1;
      matriz[i-1][j]=1;
    }
  }
  for(i=0;i<tam;i  ){
    for(j=0;j<tam;j  ){
      printf("%i ", matriz[i][j]);
    }
    printf("\n");
  }
}
```
`
```

CodePudding user response:

matriz[tam-i] will resolve to matriz[12] when i = 0. This is the out of bounds write, and the source of the bug

CodePudding user response:

Fixing the for(i = 0; i<6;i ) to for(i = 1; i<6;i ) should work, if that's the result you're trying to hv

  •  Tags:  
  • c
  • Related