1

Let us suppose the following piece of code:

#include <stdio.h>
int main(){
    int array[3] = {1,2,3};
    printf("%d\n",array[-1]);
    printf("%d\n",array[4]);
    return 0;
}

In the two printfs I'm accessing out-of-bounds positions of the array. Is there any difference in the kinds of errors generated in the two situations?

I have seen in some discussions that accessing positions before zero can produce a segmentation fault or printf can print undefined values. On the other hand, some people say that segmentation fault is not possible to happen when accessing positions after the last valid position, due to the way the operating system manages the memory. Is this correct?

And what can happen in embedded systems, without an OS?

Zaratruta
  • 2,097
  • 2
  • 20
  • 26
  • 6
    They both result in *undefined behavior*. All bets are off. – Ian Abbott Jun 29 '23 at 19:30
  • 7
    I note that you are accessing `array[4]`, but of course accessing `array[3]` would also be out-of-bounds. `&array[3]` is valid, but `array[3]` is invalid. – Ian Abbott Jun 29 '23 at 19:34
  • 2
    There's no difference between the two cases. Segmentation fault isn't guaranteed for any out-of-bounds access. Memory is allocated in page units, you get a segfault if you access outside the allocated pages, which doesn't necessarily correspond directly with array bounds. – Barmar Jun 29 '23 at 19:34
  • 4
    The *"some people say that segmentation fault is not possible to happen when accessing positions after the last valid position"* is **false**. – Weather Vane Jun 29 '23 at 19:37
  • 1
    One can easily craft an example of both cases to cause some sort of memory violation on a specific system. Consider the array to be of size of a single memory page and aligned to it. The adjacent pages are not mapped or mapped as not readable/writable. So accessing anything just a bit outside the array will cause access violation. – Eugene Sh. Jun 29 '23 at 19:50
  • " Is there any difference in the kinds of errors generated in the two situations?" - note that you may well get no error at all. Your code may function correctly. Or it may appear to function correctly for a while, then fail. Or function correctly until you change to a different compiler. Or fail only on Tuesdays. – pmacfarlane Jun 29 '23 at 21:40

1 Answers1

0

Is there any difference in the kinds of errors generated in the two situations?

Perhaps. Both are undefined behavior (UB). There is no C definition that such code acts the same or different. A particular type of UB may be more likely, yet again, it is undefined.

some people say that segmentation fault is not possible to happen when accessing positions after the last valid position, due to the way the operating system manages the memory. Is this correct?

No. A segmentation fault is possible in either case. @Weather Vane

what can happen in embedded systems, without an OS?

Various forms of UB are possible. Embedded systems tends to be more tolerant and simply print that adjacent memory, yet nasty UB is still possible. Much depends on the compiler.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256