Can a C/C++ program seg-fault from reading past the end of an array (UNIX)?

后端 未结 3 577
野趣味
野趣味 2021-01-18 02:33

I\'m aware that you can read past the end of an array - I\'m wondering now if you can seg-fault just by performing that reading operation though.

int someint         


        
3条回答
  •  失恋的感觉
    2021-01-18 03:00

    Yes, it can segfault if memory at that address is not accessible by the program. In your case it is not likely as array is allocated on stack and is only 100 bytes long and stack size is significantly larger (i.e. 8 MB per thread on Linux 2.4.X), so there will be uninitialized data. But in some cases it may crash. In either case, this code is erroneous and profilers like Valgrind should be able to help you troubleshoot it.

提交回复
热议问题