Error Stack smashing detected in C

0

Could anyone help me with the following question:

Construct a program that fills a 6x4 array with integers, computes and shows how many elements in that array are greater than 30, and then assemble a second array with elements other than 30. Instead of the number 30, the second matrix, put the number zero.

I have the following code but it is giving the following error: stack smashing detected: terminated and I am not able to 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);
}
    
asked by anonymous 07.06.2018 / 22:26

1 answer

1

The error indicates that you are moving beyond the boundaries of the array, and write to memory zones that exceed the limits.

This occurs in% w / o of% constructing 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 for and then inside it retests matriz[i][j] <= 30 . But if the element is matriz[i][j] < 30 it increases <=30 and pL , and these two are used as matrix indexes:

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

Then in your example of array pC , where the valid positions are from 2x2 to 0 , if you get three elements less than 1 30 and pL already go in% with%. This will result in an assignment equivalent to:

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

That is incorrect because it passes the boundaries of the array.

Instead I suggest you do it 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 pC are done with 3 to place the element in the same position as the resulting array.

See this example working on Ideone

Edit:

To show only values that are less than or equal to mat2 , you need to add [i][j] to the part that shows:

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

    
07.06.2018 / 23:18