Pointers and structs Java to C++
There are two important concepts in C and C++ that are not really present in Java. These are pointers and structures, or structs. These concepts exist in Java but are hidden and taken care of automatically. In C++ they are exposed. This gives the programmer more control but also requires that the programmer pay more attention. These two ideas are very important to understanding C and C++ programs.


*C++[4] means C++ example line 4.

In order to understand how pointers work, one must understand how memory works on the stack (where the local variables of functions and methods are stored) and on the heap (where dynamically allocated data is stored). In Java all simple variables like int, double or char are stored on the stack and all objects are stored on the heap. In C++ you must explicitly control this.

The following figure shows what the data will be like when the program has executed to line C++[26].

C++[12] declares a simple integer variable i and assigns the number 4 to it. We see that i is on the heap with its appropriate contents.


C++[13] declares a variable ip which is not an integer but a pointer to an integer. This is indicated by the * which states that it is a pointer. The phrase new int at C++[13] will cause enough space to be allocated on the heap to store an integer. C++[14] then assigns 7 into the space that ip is pointing to. Note that ip=7; would be an error because ip is not an integer. It is a pointer to an integer. The expression *ip is an integer because *ip means "the thing that ip points to." C++[14] assigns 7 into the location that ip is pointing at. C++[15] shows the difference between how we reference the value stored in i and how we reference the integer stored in *ip.


A C++ struct can be thought of as a primitive form of Java class. At C++[4-8] a struct named point is declared that has two fields x and y. These fields behave pretty much like fields in a Java class except that they are always public.

When working with structs it is important to pay attention to pay attention to how pointers are used. At C++[17] a variable p1 is declared as a point. Note in the figure that all of its fields (x and y) are placed on the stack. This is a fixed allocation that always occurs. C++[18-19] assign values to the fields in the same way as we would do in Java using the dot notation. C++[20] shows those fields being used in the same way as we would in Java. There are no pointers involved with the variable p1. This cannot happen in Java. For an object of a class in Java, a pointer is always used even though it is hidden from the programmer.

At C++[22] the variable pp is declared as a pointer to a point. Note that pp is not a point. It is a pointer. C++[22] also shows us creating new space on the heap to store a point and assigning a pointer to that space into pp. Looking at the figure we see that pp does not have space for x and y on the stack. Only the pointer is on the stack. The space for x and y is in the point struct on the heap. Because pp is not a point but rather a pointer to a point we do not use the dot notation. Instead we use the arrow notation to access fields (pp->x). C++[23-24] shows the values 14 and 75 being assigned to the fields of *pp. The arrow notation works the same as the dot notation except that it is applied to pointers rather than the struct itself.


A major difference between C++ and Java is that C++ does not automatically clean up space on the heap. In Java, when we are no longer using an object that has been allocated using new we just ignore it and the system cleans it up. In C++ there is no automatic cleanup. The programmer must explicitly clean up heap storage. C++[27-28] show the storage for ip and pp being freed using the delete operation. IT IS VERY IMPORTANT THAT YOU NOT FORGET TO CLEAN UP YOUR SPACE.