Unlike the Java runtime system, the C++ runtime system has no intrinsic way of knowing the size of an array, even if it has been declared with a compile-time constant size. The primary differences related to using arrays in C++ as opposed to using them in Java are due to this very significant difference.

As a concrete example, you know you can write a Java method to return the sum of the numbers in an array as follows:

        double sum(double[ ] buf) // Java version
        {
            double s = 0.0;
            for (int i=0 ; i<buf.length ; i++)
                s += buf[i];
            return s;
        }

Moreover, any attempt to access an array location outside the bounds of the array will result in an immediate runtime exception being thrown.

Since the C++ runtime system cannot determine the length of an array:

  1. There is no way to query the length inside sum as you can in Java (e.g., by writing buf.length).
  2. All attempts to read or write array positions outside the bounds of the allocated array will be performed, even if the index is negative! Unless the index is way out of bounds (i.e., a very large negative or positive number), no exception will be thrown – or even detected – when the invalid access is performed. Typically your program will at best produce unexpected results; more likely it will crash later when control has moved to some unrelated part of your code, thereby making it all the more difficult for you to determine why your program has crashed.

Therefore you must make sure that any code processing an array has a way of ensuring that it only uses valid indices. There are many conventions for that. One very common approach when passing arrays to methods is to pass not only the array itself, but also its length. For example, a typical way to translate the array summing code we saw above from Java to C++ would be as follows:

        double sum(double buf[ ], int bufLength) // C++ version
        {
            double s = 0.0;
            for (int i=0 ; i<bufLength ; i++)
                s += buf[i];
            return s;
        }

Notes:

  1. Obviously, the caller of such C++ functions and methods must pass not only the array, but its length as well.
  2. There is a small but important syntactical difference: "double[ ] buf" (in Java) versus "double buf[ ]" (in C++). (We will see another way to declare C++ arrays on a later page in this web site.)
  3. Since the C++ runtime cannot determine the length of arrays, there is no construct in C++ analogous to Java's "for each":
            double sum(double[ ] buf) // Another Java version using the "for each" construct
            {
                double s = 0.0
                for (double v : buf) // "for each v in buf": supported in Java, but not in C++
                    s += v;
                return s;
            }
  4. While conventional C++ arrays do not know their size, several array-like classes in the STL (standard template library) do know their size and can be used more like Java arrays. For example, std::array, std::vector, and std::initializer_list behave this way and can be used (since C++11) with this "for each" construct.

Array Initialization

C++ arrays can be initialized using a brace notation in very much the same way as they are in Java:

JavaC++
double[] x = new double[] { 1.1, 2.2, 3.3 };
double x[] = { 1.1, 2.2, 3.3 };