Array bigger than allocated?

I have an array that is declared as char buff [8]. It should be only 8 bytes, but looking at the assembly and testing of the code, I get a segmentation error when I enter something more than 32 characters into this character, while I expect it to be more than 8 characters. Why is this?

+2
source share
5 answers

What you are saying does not contradict:

  • You have room for 8 characters.

  • You get an error when entering more than 32 characters.

So that?

The fact is that no one told you that you are guaranteed to receive an error message if you enter more than 8 characters. This is just undefined behavior , and anything can (and will) be.

You absolutely must not think that the absence of obvious incorrect behavior is proof of the correctness of your code. The correctness of the code can only be checked by checking the code for compliance with the rules of the language (although some automated tools, such as valgrind , are of great help).

+9
source

Writing outside the array is undefined behavior. undefined behavior means nothing (including segmentation error) is guaranteed .

In other words, he can do anything. More practical, most likely, the record did not concern anything protected, therefore, from the point of view of the OS, it is still normal to 32.

This brings up an interesting point. What is “completely wrong” from the point of view of C may be ok with the OS. The OS only cares about which pages you access:

  • Is the address displayed for your process?
  • Does your process have rights?

You should not expect the OS to pat you if something goes wrong. A useful tool for this (slapping) is valgrind if you are using Unix. It will warn you if your process does unpleasant things, even if these unpleasant things are technically OK with the OS.

+5
source

Your char buff[8] declaration sounds like a dedicated stack variable, although it could be a heap allocated if it is part of a structure. Access outside the array is undefined behavior and is known as buffer overflow. Buffer overflows on allocated memory in the stack can damage the current stack stack and possibly other frame stacks in the call stack. With undefined behavior, anything can happen, including without a visible error. You would not expect a seg crash right away, because the stack usually starts at the beginning of the stream.

For heap-allocated memory, memory managers typically allocate large blocks of memory and then allocate from these larger blocks. This is why you often don't get a seg error when accessing outside the memory block.

Undefined behavior has access to the end of a block of memory. And quite rightly, according to the standard, so that such access beyond the limits of access leads to seg failures or, apparently, really successful reading or writing. I argue that it seems to be successful, because if you write, you can very well do cumulative damage by writing borders.

+2
source

C arrays do not have border checks.

Like others, you click on undefined behavior; until you stay within the boundaries of the array, everything will be fine. If you cheat on the standard, anything can happen, including your program, it seems to work correctly, as well as the explosion of the Sun.

What happens in practice is that with the variables associated with the stack, you are likely to overwrite other variables in the stack, getting "impossible" errors, or if you press the canary value set by the compiler, it can detect overflow buffer upon return from function. For variables allocated in the so-called heap, the heap allocator may have given more space than requested, so an error can be less easily detected, although you can easily spoil the internal structures of the heap.

In both cases, you can also get to the protected memory page, which will force your program to stop (this is less common for the stack, because you usually have to rewrite the entire stack to go to the protected page).

+2
source

If you do not tell us something that you answered your question, please.

ads

 char buff[8] ; 

means that the compiler captures 8 bytes of memory. If you try to add 32 char to it, you should get a seg error that caused a buffer overflow.

Each char is a byte (unless you make the unicode in which the word is), so you are trying to put 4x the number of characters that will fit in your buffer.

Is this your first coding in C?

-1
source

All Articles