Stack error Smashing Detected in C

Asked

Viewed 8,098 times

0

Could someone help me on the following question:

Create a program that fills a 6x4 matrix with integers, calculate and show how many elements of this matrix are greater than 30, and then assemble a second matrix with the different elements of 30. In place of number 30, of the second matrix, put the number zero.

I made the following code but this giving the following error: stack Smashing Detected: terminated and I’m not getting to know why. I’m using Netbeans as an 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);
}

1 answer

1


Error indicates you are crossing matrix boundaries, and write to memory zones that cross boundaries.

This happens in the for building the second matrix:

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 whether matriz[i][j] <= 30 And then inside it goes back to testing matriz[i][j] < 30. But if the element is <=30 increases the pL and the pC, and these two are used as matrix indices:

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

Then in your matrix example 2x2, where the validated positions are 0 to 1, three elements smaller than 30 the pL and the pC are already in vain 3. This will result in an assignment equivalent to:

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

Which is incorrect because it crosses the boundaries of the matrix.

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++;
            }
        }
    }
}

The assignments in mat2 are made with [i][j] to place the element in the same position as the resulting matrix.

See this example working on Ideone

Edit:

To show output only values less than or equal to 30 needs to add a if in the part showing:

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

  • Fight man, but I still don’t understand how I’m going to make the control of the for that will print the second matrix. Could help me in this tbm?

  • @Thiagohenriquedomingues No problem. Which control ? The printing of the second matrix is not as you want ? What is missing ?

  • In order to count the values <= 30. Because I created a 2x2 matrix for tests only, the exercise requires a 6x4. In case you would need a variable to count these values, for when printing do not print every matriz2;

  • I don’t know if it’s clear.

  • @Thiagohenriquedomingues The size of the matrix does not influence logic. And the count of those over 30, under 30 and equal to 30 is already being done. Which count is missing ? " for when printing not to print every matriz2" - which ones are not to print ? Could you give an example of a correct print ?

  • Input: 50, 6, 1 ,30 | Expected exit: 6,1,0 | Exit obitida: 654948592,6,1,0

  • @Thiagohenriquedomingues But the 50 doesn’t show up on the way out because it’s bigger than 30 that’s it ?

  • I would like you to print some values less than or equal to 30 (where it will be replaced by 0)

  • Especially, I wanted to find a way to skip the position 50 is and print the others

  • @Thiagohenriquedomingues I already edited the answer to contemplate this part as well. See if that’s what I was looking for.

  • Exactly, I was going to tell you that I had already solved, I did the same thing that you did.

  • Thank you so much for your help, man, and I’m sorry about the haha grind

  • @Thiagohenriquedomingues No problem. We’re here to help :)

Show 8 more comments

Browser other questions tagged

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