Short answer is that in bounds behavior is specified by the standard and is reliable. Out of bounds behavior is not specified and can differ from one platform to another.
Why does this for loop exit on some platforms and not on others?
I have recently started to learn C and I am taking a class with C as the subject. I'm currently playing around with loops and I'm running into some odd behaviour which I don't know how to explain. #include <stdio.h> int main() { int array[10],i; for (i = 0; i <=10 ; i++) { array[i]=0; /*code should never terminate*/ printf("test \\n"); } printf("%d \\n", sizeof(array)/sizeof(int)); return 0; }On my laptop running Ubuntu 14.04, this code does not break. It runs to completion. On my school's computer running CentOS 6.6, it also runs fine. On Windows 8.1, the loop never terminates. What's even more strange is that when I edit the condition of the `for` loop to: `i <= 11`, the code only terminates on my laptop running Ubuntu. It never terminates in CentOS and Windows. Can anyone explain what's happening in the memory and why the different OSes running the same code give different outcomes? EDIT: I know the for loop goes out of bounds. I'm doing it intentionally. I just can't figure out how the behaviour can be different across different OSes and computers.
Follow
1
Add comment
More
Report
1 Expert Answer
Still looking for help? Get the right answer, fast.
Ask a question for free
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Find an Online Tutor Now
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.