Matt Weisfeld, author of The Object-Oriented Thought Process, Fourth Edition, discusses the advantages and hazards of using pointers to access and manipulate memory addresses directly. The risks are significant enough that some languages allow this feature, while others expressly prohibit it. What's so dangerous - and tantalizingly powerful - about pointer arithmetic?

The reason that this C++ application "falls over the cliff" has to do with the fact that the for loop literally blows past the edge of the array and, very inelegantly, tramples on memory that doesn't belong to it. The possibility of code "trampling" on inappropriate memory occurs because C++ allows the use of pointer arithmetic—which means, among others things, that a programmer can directly access and manipulate memory addresses.

As Figure 2 shows, the application literally made it as far as myArray[4095] before the runtime even bothered to notice. And when the runtime finally paid attention—it wasn't happy.

Interestingly, many programmers who began their careers after the year 2000 have never used a development environment that would even allow code like this. Programmers who develop primarily with modern object-oriented development environments don't have pointer arithmetic like this in their toolkit. Java, .NET, C#, Visual Basic, and so on simply don't allow direct manipulation of memory locations.

NOTE

Due to the rise in iOS development (used in Apple's iPhone, iPod, iPad, and so on), the use of Objective-C is now a significant part of the modern development mix (with Swift on the horizon). Objective-C (which is based on C) is an object-based language. Although you can certainly write object-oriented code in Objective-C, an object-oriented framework isn't strictly enforced. Thus, both Objective-C and C++ fall into the category of object-based languages, and both support pointer arithmetic.