So, I just studied the Arrays material and got a problem...
I got this following code:
int a[5];
int i;
for(i=0; i<=10; i++) {
What you are doing here is called Undefined Behavior. It will not show an error when you go beyond the index of an array.
This means that the behavior that your code will show, cannot be defined.
On a side note, I would like to add something I read somewhere. Please note that this is just something I read, it may be wrong, but it does make sense. In C
, some behaviors are not made into errors, because C
believes in that the programmers are smart enough not to make mistakes like this.
C doesn't have any standard on constraint on array index out of bound checking.
That means programs are free to violate the index rule. It is upto the programmers to check the array index constraint.
And, that will result in an undefined behaviour.
This error has nothing to do with the compiler. Whether it's C or Java, as long as it does not violate the language syntax and semantic, the code will compile.
What you have is a logical error which could be detected by the language run-time environment, e.g. JVM, which what C lacks.
C does not perform array bounds checking.
C does not do any bounds checking on array accesses (that is, the compiler will not inject any code that will throw an exception if you try to access an element outside of the array bounds). The language standard does not define what should happen if you access items outside of the array bounds; you basically void the warranty on your code when you do that.
The result can include anything from running to completion with no apparent errors, to throwing a segfault, to triggering a malware routine, etc. Again, the language definition does not mandate any particular behavior for such an error.
Remember that C is a product of the early '70s; bounds checks slow you down, and the C philosophy is that the programmer is in the best position to know whether a bounds check is really necessary or not. The compiler assumes you know how big your array is, and that you know not to try to read or write past the end of it.