Error of memory allocation

Asked

Viewed 84 times

3

I am creating a C script to classify a triangle according to the 3 sides passed. For this I use three distinct variables, lado1, lado2 and lado3, as illustrated by the code below:


#include &ltstdio.h>
#include &ltstdlib.h>
int main(int argc, char *argv[]) {
    unsigned short int lado1, lado2, lado3;

    printf("L1: ");
    scanf("%i", &lado1);
    printf("L2: ");
    scanf("%i", &lado2);
    printf("L3: ");
    scanf("%i", &lado3);

    if (lado1 < (lado2 + lado3) && lado2 < (lado1 + lado3) && lado3 < (lado1 + lado2)) {
        if ((lado1 == lado2) && (lado2 == lado3)) {
            printf("Triângulo equilátero");
        } else if ((lado1 != lado2) && (lado2 != lado3)) {
            printf("Triângulo escaleno");
        } else {
            printf("Triângulo isóceles");
        }
    } else {
        printf("Os lados informados não constituem um triângulo");
    }

    return 0;
}

However, when I try to compile this code a memory error is shown:

stack smashing detected: <unknown> terminated

The same does not happen when I declare a string along with the other three variables lado1, lado2 and lado3. Thus, in addition to the three variables I have another variable of type string folk[90] which is not used within the code at any time, but this time I have the desired return.

Follows the code:


#include &ltstdio.h>
#include &ltstdlib.h>
int main(int argc, char *argv[]) {
    unsigned short int lado1, lado2, lado3, folk[90];

    printf("L1: ");
    scanf("%i", &lado1);
    printf("L2: ");
    scanf("%i", &lado2);
    printf("L3: ");
    scanf("%i", &lado3);

    if (lado1 < (lado2 + lado3) && lado2 < (lado1 + lado3) && lado3 < (lado1 + lado2)) {
        if ((lado1 == lado2) && (lado2 == lado3)) {
            printf("Triângulo equilátero");
        } else if ((lado1 != lado2) && (lado2 != lado3)) {
            printf("Triângulo escaleno");
        } else {
            printf("Triângulo isóceles");
        }
    } else {
        printf("Os lados informados não constituem um triângulo");
    }

    return 0;
}

What I want to know is the explanation for this, why does this occur? and how to allocate memory correctly without needing this fourth variable?

The compiler I use is the gcc

  • I could not replicate the error. Both codes worked. I passed values 3,4 and 5

  • @Augustovasques but which compiler do you use?

  • I used a container with gcc

2 answers

3

First it must be understood what is the stack or stack:

The stack is a contiguous portion of memory reserved to stack the necessary data while executing code blocks.

Each need for allocation is a chunk of the stack that is always used in sequence... 1

In the case of your program 3 variables of type unsigned short which has its size set at least 16 bits, or 2 bytes, according to the wikipedia.

So we can see the stack of your show, like:

pilha

Stack Smashing is a type of attack, a buffer overflow, where the program attempts to extrapolate space (or stack), reserved for him. 5

And that’s what you did, reading 4 bytes in scanf("%i", &lado3);, i.e., by typing an integer in the address 0x05.

prilha + execução

When he declared folk (NEVER DO THAT for a running problem, NEVER EVEN), the program has had slack, and may not violate the running stack, but even so, there is an invasion of memory (quite damaging as well).

In short, your program, even if it runs, doesn’t work.

To fix, align the input data type and the variable that receives its value, such as:

int lado1, lado2, lado3;
  • Sorry, man, I didn’t get the address right, it sounded confusing to me, especially this part where you talk about reading 4bytes in scanf("%i", &lado3), for me each of the variables should store a small integer and not negative, precisely why I used the unsigned short int. Now I understand that I have to worry about the amount of bytes that each type carries.

  • Still, in my view each of these variables should load 4bytes, right?. But from what you explained to me, it’s not like that. You would have some reference that would help me understand this, or you could explain it better?

  • Understand the data types in C is a start.

2

scanf expecting a *int to store the value of %i, you provide a *unsigned short int.

It is likely that:

sizeof(int) == 4
sizeof(unsigned short int) == 2

Thus, the CPU will write the result of size four in a region of size two and stack smashing detected: <unknown> terminated

The array folk[90] may be being allocated shortly after lado3 in memory and, coincidentally, prevents the value from being written in a region that breaks the code.

Change the type of variables:

int lado1, lado2, lado3;

Or the guy from scanf for %hu:

scanf("%hu", &lado1);

Reference: http://www.cplusplus.com/reference/cstdio/scanf/

  • Change the type to int worked. Already the second way did not give, the problem continued the same. Thank you very much for the help!

  • Oops, it was a typo of mine. The %u is for unsigned, the %h is for short. You can join the two. I fixed the answer.

  • 1

    Thank you very much, it worked with using scanf("%hu", &lado1);

  • great =) if you have solved your question, please choose one of the answers and mark the question as answered.

Browser other questions tagged

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