Error: Stack Smashing Detected in C

Asked

Viewed 58 times

2

I have to make a code in which the user will give me the size of the matrix and the numbers within that matrix, and the size of the matrix has to be greater than or equal to 2 and the numbers between 0 to 9.

When I do the matrix size test, if I use a number greater than 2 and a smaller one, it gives the Stack Smashing Detected error. What I’m doing wrong?

#include <stdio.h>
#include <locale.h>

int main()
{
    setlocale(LC_ALL, "Portuguese");

    int linha, coluna, i, j;

    printf("Quantidade de linhas: ");
    scanf("%d", &linha);
    printf("Quantidade de colunas: ");
    scanf("%d", &coluna);

    int matriz[linha][coluna];

    if (coluna >= 2 && linha >= 2)
    {
        for (i = 0; i < linha; i++)
        {
            for (j = 0; j < coluna; j++)
            {
                printf("Números da matriz: ");
                scanf("%d", &matriz[i][j]);

                if (matriz[i][j] < 0 || matriz[i][j] > 9)
                {
                    printf("\nErro.\n");
                    return 1;
                }
            }
        }
    }
    else
    {
        printf("\nErro.\n");
        return 1;
    }

}
  • It ran as expected. https://ideone.com/qpnJks

  • The is all correct and I even ran into Valgrind to confirm and all memory was misplaced correctly. With what values did you specifically see this problem ? It was in the code you have in the question or in an earlier version ?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.