Accessing out-of-bounds elements of dynamically al

2019-03-03 01:50发布

问题:

I'm developing a program in C that uses an array of linked lists (a primitive hash table) as a data type to represent certain date information. The array has twelve elements corresponding to the months of the year, and each month has a linked list that contains nodes of data.

I developed the module that used this data type and it worked fine. I later discovered that I was accessing array elements that were out of bounds (for example accessing the 12th element by the index 12 instead of 11). But the program worked consistently without incident. I never received a segmentation fault. I have since corrected the coding error. Could anybody explain why accessing out-of-bounds elements would not result in a segfault?

This is not the first time it has happened. I have created a dynamically allocated multidimensional array, and for the sake of testing, I tried to access out-of-bounds elements. The program ran fine, produced accurate results, and did not seg fault in most circumstances. The only time I achieved one, I had to try accessing substantially out-of-bounds elements.

(These programs currently are windows console applications for testing. I am compiling with MinGW. I can include code, if it would be helpful.)

回答1:

In C, accessing an array outside its bounds is undefined behavior.

That means that anything can happen, including the program behaving as you might expect it to.

The C language doesn't require bounds checking on array accesses, and most C compilers don't implement it.

For example, suppose you declare:

int before;
int array[10];
int after;

The order in which these are stored in memory is undefined, but suppose they're stored contiguously in the order they're declared. If you attempt to access array[-1], you might access before instead. If you attempt to access array[10], you might access after instead.

The burden is on the programmer to avoid accessing arrays outside their bounds. Or there might not be anything allocated before and/or after your array.

An analogy: "The sign says I'm only allowed to cross the street when the light is green. I crossed on red, and nothing happened. Why didn't a car hit me?" (There are languages that go out of their way to make the car hit you. C isn't one of them.)



回答2:

Could anybody explain why accessing out-of-bounds elements would not result in a segfault?

This is Undefined Behaviour, it does not have to segfault. On Linux you can run your program under Valgrind to catch this kind of errors.



回答3:

SegFaults are OS creatures. They are thrown when a process tries to access memory that does not belong to it, and are not part of the C language. In C, accessing out of bounds elements is just undefined behavior, which means it might fail, and it might not. If, for instance, the memory allocator has provided a memory chunk larger than what you've asked for the array, the OS won't care if you go out of bound, because you'll be accessing memory that does belong to your process. When this is the case, you just get a bug in your program.



回答4:

Accessing data by indexing out of bounds of an array is undefined behavior. In most cases (particularly dynamically allocated memory), accessing data "near"(not too far out of bounds) the array won't segfault for various reasons. The memory may be allocated in rounded chunks, larger than what you requested or the "malloc" implementation may put some arbitrary bookkeeping information etc. The end result being that that chunk of memory is mapped, though may contains data ranging from garbage to important booking keeping info. Don't rely on this behavior.