-6

In C, I noticed I can write to static array off-limits, for example:

static char a[10] = {0};

for (int i=0; i<20; i++) {
    a[i] = 'a'; // Should fail when i > 9
}

I expected to get segmentation fault but it executes just fine.

If static arrays were allocated on the stack, it would make sense, but they're not, so why is that so?

Note: static int arrays behave similarly. Didn't check other types.

Thanks.

Edit: This is not a duplicate since the other questions were not about static arrays. Unlike "regular" arrays, static arrays are allocated in BSS. The behavior might be different, which is why I'm asking separately.

Raz Moshe
  • 59
  • 1
  • 6
  • 1
    Can you explain why you would expect it to result in an exception? It's hard to correct your misunderstanding if we don't know what you misunderstand. I can't think of any reason you would expect an exception. Are you imagining that individual bytes can be write-protected or something? – David Schwartz Jun 16 '19 at 07:36
  • 2
    Welcome to the world of undefined behavior. – kiran Biradar Jun 16 '19 at 07:37
  • @DavidSchwartz, I'm writing to an address that I didn't ask allocation for. Shouldn't it result in some kind of exception? – Raz Moshe Jun 16 '19 at 07:48
  • @RazMoshe Do you think that when you allocate two or three bytes, the system somehow makes just those two or three bytes accessible and the next few bytes are somehow write protected? That's not how memory management works. – David Schwartz Jun 16 '19 at 07:51
  • @DavidSchwartz, Are you talking about the memory alignment? I know about it, and I understand that I might just write to some bytes that were actually allocated for me (even though I didn't ask for them). I want to know if this is the reason for no exception or there's something else. – Raz Moshe Jun 16 '19 at 07:55
  • 1
    @RazMoshe Memory protection is done in pages which are probably 4KB minimum on your system. You're not going to get an exception if you don't cross a page boundary since the addresses will all have the same memory protection. Performance would be atrocious if every time any thread ever tried to allocate or free a byte, OS-level memory protection had to be altered for process memory. It's unreasonable to expect memory protection violations. – David Schwartz Jun 16 '19 at 08:01
  • @DavidSchwartz. "It's unreasonable to expect memory protection violations"- that is, until I write off-limits so much until the next page, where I should expect an exception. Is that right? – Raz Moshe Jun 16 '19 at 08:14
  • @RazMoshe It's more reasonable to expect one there. But you never know. The memory allocator might try to save overhead and allocate very large chunks at a time. It's still unreasonable to expect it to do extra, unnecessary work just to make code that has a bug fail differently. – David Schwartz Jun 16 '19 at 08:21
  • @DavidSchwartz Understood. Thank you very much. – Raz Moshe Jun 16 '19 at 08:45
  • You can only expect an exception if someone has promised you will get one. Can you quote the promise and indicate which document it comes from? If not, then you may or may not get an exception. Note C doesn't have a notion of exception at all. – n. m. could be an AI Jun 16 '19 at 09:17

1 Answers1

2

You will only get a segmentation fault when you actually attempt to write to memory that is an illegal address. Your example code writes beyond what you allocated for the array, but that isn't an address beyond what the OS determines is legal for you to use. Even if you do not get a segmentation fault, your example code could corrupt other data structures in your code and cause major faulty behavior of a program, and possibly even worse, it can cause intermittent and difficult to debug faulty behavior.