Arrays Java to C++
Arrays are an important part of computing and in many ways Java and C++ are similar. However, there are fundamental differences. C++ has a wierd language structure that makes arrays and pointers the same. This makes sense when looking at the underlying implementation but as a language design it can be confusing. Both Java and C++ index and use arrays in similar ways. In C++ it is possible to create an array without dynamic allocation but in general both use new to allocate space for an array. In C++ the space allocated to an array is not automatically garbage collected and so must be dealt with in a class' destructor. Also in C++ there is no bounds checking of an array. This is more efficient (sometimes) but is very unsafe. You must be very careful not to use a negative index and not to subscript beyond the end of an array.



*[2] means the file "" at line 2.

In Java, all array variables are actually pointers to arrays and all object variables are actually pointers to objects. This is not true in C++.

Simple arrays of strings

At[3] a simple array of strings is initialized with three strings. This is the same at Main.cpp[7]. They way these two are used is also identical between[16] and Main.cpp[20].

Arrays of objects

There are some differences, however, when arrays store objects. Note that the array someStudents at[7] is created as a new array with three slots in the array. The Java version of someStudents is a pointer to the array. The version of someStudents declared at Main.cpp[11] does not use a new operator. This array has its space allocated directly on the stack. However, at Main.cpp[11] we explicitly declare this array as holding pointers to Students rather than actual Student objects. The (*) in Main.cpp[11] indicates that these are pointers to students. Because we used pointers to students we fill the arrays at[8-10] exactly the same as at Main.cpp[12-14]. The use and indexing of these two arrays at[13] and Main.cpp[17] are also the same. Note, however, that in C++ the add method at Course.h[15] takes a pointer to a Student rather than a simple Student object.

If an array is dynamically allocated (as all Java arrays are) then the process is a little more complicated. In C++ arrays are treated like pointers. For the variable students at Course.h[12] we want a pointer to an array of pointers to objects. To do this we declare students to be a pointer to a pointer. Note the ** at Course.h[12]. In the constructor for Course at Course.cpp[7] we create a new array of pointers to students (Student*) that has MAX_STUDENTS in the array. This is the implementation that most closely mirrors what Java does with its arrays of objects.


Remember that in C++ we must reclaim any storage that we allocate using a new. At Main.cpp[24] we use the delete to allocate the storage that we created for the variable CS142 at Main.cpp[9]. Before the storage is deallocated, the destructor for the Course class is called at Course.cpp[25]. The first thing this destructor does is go through the students array and deallocate all of the students in the course. This is done by the for loop at Course.cpp[26-27]. After all of the students have been deleted, then the array itself must be deleted. Note the special form of delete for arrays that is shown at Course.cpp[28].