Why does the program work despite the invasion of memory in the vector?

Asked

Viewed 78 times

0

#include <stdio.h>
#include <stdlib.h>

int main()
{
 char s[2];
 int i;
 printf("Entre com os caracteres\n");
 for(i=0;i<4;i++)
 s[i]=getche();
 printf("\n\n");
 for(i=0;i<4;i++)
 printf("%c ", s[i]);
 printf("\n\n");
 system("pause");
 return 0;
}
  • Each compiler can, depending on the machine architecture, leave some unallocated byte(s)/filled(s) to optimize the alignment of the allocated variables. The C language considers that the programmer knows what he is doing and does not suppress, for example, the overflow of memory. You may get lucky and the array overflows into these unused bytes, but it can bring bad luck and fall into a memory area used by another variable.

  • 1

    Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

2 answers

1

I didn’t even analyze the code to see the exact problem, but what people need to keep in mind is that C is a permissive language, it lets almost everything happen, and it’s the programmer’s problem to take care not to give trouble. Some compilers even have options to better handle certain situations, libraries help a little not happen terrible things when you use their functions, have external tools, but the programmer must know what you are doing and be in control of everything. This is the biggest advantage of C, so it is fast and flexible, but charges the price.

In a way it’s good because it creates serious problems and it makes people who don’t get along with programming so much that they get away from it as it makes them realize that not everything that works is right. When the person understands this and puts a strong focus on his work always considering it he ends up turning professional with capital P.

Languages that are permissive in things that do not cause such serious problems addict the "programmer" in making it work, even if it is wrong.

C has no robust memory controls, you allocate a buffer and it’s your problem to manage it. If you screw up your bad luck. And if nothing else causes trouble it looks right, and it works, it doesn’t make a mistake, but it either comes out unexpected or it’s all right by coincidence. In exercises it is very common to be right by coincidence after the code is so simple that it does not generate conflict, and it is exactly what happened in this code, it is so small that it did not even have opportunity to corrupt the memory, but start doing something else and it will happen.

Memory is there for you to use with freedom. Great powers, great responsibilities. You put it in an undeclared area, the compiler lets, if you don’t use the outdated area, ok, if you declare something else there and use it will get data loss.

Technically there was no memory invasion because C has all memories of the process at his disposal, conceptually yes, there was intrusion, and concepts fix us doing it the right way.

1

@Maniero has already explained the problem itself, but I want in my response to focus on its code and conclusion.

You start by saying:

Why the program works despite the invasion of memory in the vector s?

But I tell you that this conclusion of having worked was hasty and does not correspond to reality. Now see the execution of the code in online C Compiler.

Notice how I came in with 4 characters:

a
b
c
d

And the exit was:

a b 

I took it and put one print of the variable i after the first for, that shows:

Valor de i depois do for: 100

As the vector s and the variable i are followed in memory, the first value read out of s, in position 2, hit the variable i, and changed its value to 100 which is the ASCII code of the letter c and made the for ends in the third element and not in the fourth. If the value of i before the second for it would already give you unexpected results. Although you have put the 4 characters only sees two effectively in the output.

In conclusion: The language C does not prevent you from writing in memory that it does not belong to you, and if you do it will have indefinite behavior, that is, anything can happen. And not even in this small example worked as expected imagine in much more extensive and complex programs.

Browser other questions tagged

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