3

In C, the code snippet:

int i;
    double x[10];
    for (i = 0; i <= 10; i++) {
        x[i] = (double) i;
        printf("%f\n", x[i]);
    }

produces the following output:

0.000000
1.000000
2.000000
3.000000
4.000000
5.000000
6.000000
7.000000
8.000000
9.000000
10.000000

However, in Java the code snippet(same code) just syntactically different:

int i;
        double[] x = new double[10];
        for (i = 0; i <= 10; i++) {
            x[i] = (double) i;
            System.out.printf("%f\n", x[i]);
        }

produces the following output:

0.000000
1.000000
2.000000
3.000000
4.000000
5.000000
6.000000
7.000000
8.000000
9.000000
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    at Array.main(Array.java:14)

Why does the C compiler not flag the attempt to access outside the array bounds? Am I missing something here?

user3575020
  • 137
  • 1
  • 7
  • In both cases, it's not the compiler checking bounds. That happens at runtime (in Java) or not at all (in C). – Thilo Jan 09 '15 at 07:01
  • you loop should work from 0 to 9. – Himanshu Jan 09 '15 at 07:02
  • Also see http://stackoverflow.com/questions/7410296/why-is-bounds-checking-not-implemented-in-some-of-the-languages – Lundin Jan 09 '15 at 07:34
  • The duplicate asks explicitly about C++. This question asks about the differences between C and Java. That is only partially and indirectly addressed by the duplicate. Voting to re-open. – juanchopanza Jan 09 '15 at 09:07

2 Answers2

6

Java has array bounds checking, C doesn't. In Java you get a runtime exception when you access index 10, in C you get undefined behaviour. In this case, you wrote 10.0 to some place you're not allowed too, but through sheer bad luck, the program ran to completion.

Also note that java arrays have a length property, which makes it easy to avoid this kind of mistake. In the C case, it is possible to get the length of the array by careful use of the sizeof operator. But the fact that often pointers are used instead of arrays (e.g. when you "pass" an array to a function that takes a pointer to an element of an array) means often the size information is not readily available.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • Is it really undefined when writing (except for the minor detail of what exactly you are overwriting when doing that)? – Thilo Jan 09 '15 at 07:03
  • 1
    @Thilo Yes, it is undefined. It is too complicated to specify what is supposed to happen in this case, without adding the overhead of a bounds checking system. – juanchopanza Jan 09 '15 at 07:04
0

In C, we can access the element that is not initialize to that variable. In java we cannot do that. It strictly check the array of bounds.

Java performs bounds checking every time an element in an array is accessed. Bounds checking is where an access to an array index is checked against the size of the array – and an exception is thrown if that array index is out of bounds, and larger than the size of the array.

C does not perform the bounds checking.

Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31