Stack smashing error detected in C

Could anyone help me with the following Question:

Draw up a program that fills a 6x4 array with integers, calculates and shows how many elements of that array are greater than 30, and then assembles a second array with the elements other than 30. In place of the number 30, from the second matrix, put the number zero.

I did the following code but it is giving the following error: stack smashing detected: terminated and I am not getting know why.I am using Netbeans as IDE.

#include <stdio.h>
#include <stdlib.h>
#define L 2
#define C 2

int main(int argc, char** argv) {
    int matriz[L][C], mat2[L][C];
    int i, j, pL = 0, pC = 0, contMaior30 = 0, contIgual30 = 0, contDif30 = 0;

    for (i = 0; i < L; i++) {
        for (j = 0; j < C; j++) {
            printf("M[%d][%d] = ", i, j);
            scanf("%d", &matriz[i][j]);
        }
    }

    //Laço para fazer a verificação dos valores digitados
    for (i = 0; i < L; i++) {
        for (j = 0; j < C; j++) {
            if (matriz[i][j] <= 30) {
                if (matriz[i][j] == 30) {
                    mat2[pL][pC] = 0;
                } else if (matriz[i][j] < 30) {
                    mat2[pL][pC] = matriz[i][j];
                }
                contDif30++;
                pL++;
                pC++;
            }

           //Contar a quantidade de números maiores que 30 
            else {
                contMaior30++;
            }
        }
    }

    //Não sei como controlar a impressão de segunda matriz
    printf("\nImpressão do segundo vetor: \n");
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++) {
            printf("\tM2: %d \n", mat2[i][j]);
        }
    }

    return (EXIT_SUCCESS);
}
 0
Author: Maniero, 2018-06-07

1 answers

The error indicates that it is passing the limits of the array, and write in memory zones that pass the limits.

This occurs in for which builds the second array:

if (matriz[i][j] <= 30) {
    if (matriz[i][j] == 30) {
        mat2[pL][pC] = 0; // <--- aqui
    } else if (matriz[i][j] < 30) {
        mat2[pL][pC] = matriz[i][j]; // <-- e aqui
    }
    contDif30++;
    pL++;
    pC++;
}

First of all the comparison is strange because it starts by testing if matriz[i][j] <= 30 and then inside it tests matriz[i][j] < 30 again. But if the element is <=30 it increases the pL and the pC, and these two are used as indices of the array:

mat2[pL][pC] = matriz[i][j];
//    ^---^

Right in your Matrix example 2x2, where the positions valid vao from 0 to 1, if you catch three elements smaller than 30 the pL and the pC already go in 3. This will result in an assignment equivalent to:

mat2[3][3] = matriz[i][j];

Which is incorrect because it passes the boundaries of the Matrix.

Instead I suggest you do it before like this:

//Laço para fazer a verificação dos valores digitados
for (i = 0; i < L; i++) {
    for (j = 0; j < C; j++) {
        if (matriz[i][j] == 30){
            mat2[i][j] = 0; //atribui em mat2 com base no i, j também
            contIgual30++;
        }
        else {
            contDif30++;
            mat2[i][j] = matriz[i][j]; //atribui em mat2 com base no i, j também
            if (matriz[i][j] > 30){
                contMaior30++;
            }
        }
    }
}

Assignments in mat2 are made with [i][j] to place the element in the same position as the resulting array.

See this example working on Ideone

Edit:

To show in the output only values less than or equal to 30 you need to add a if in the showing Part:

for (i = 0; i < 2; i++) {
    for (j = 0; j < 2; j++) {
        if (mat2[i][j] <= 30){ // <--este
            printf("\tM2: %d \n", mat2[i][j]);
        }
    }
}

See Also this example in Ideone

 1
Author: Isac, 2018-06-07 21:56:09