Memory as a Programming Concept in C and C++

A static one-dimensional array in C/C++ is represented by a constant pointer, which holds the base address of the array and points to a statically allocated memory segment that stores the array items. The indexing expressions of type x[i] are translated by C/C++ compilers into indirection expressions of type *(x+i) . For these reasons, the size of an array is unknown at run time and so a run-time index range check cannot be performed. Only a simple compile-time index range check is performed by C/C++ compilers (e.g., during initialization). This philosophy is deliberate , a response to the significant costs (as in Pascal) of run-time index range checking. Another consequence of the representation of arrays as pointers concerns passing of array arguments to functions. Though C can only perform passing by value and though the C++ default is also passing by value (passing by reference must be explicitly stated), it is actually the pointer representing the array that is passed by value, and this value (the so-called base address of the array) is then used to access the actual array. This results in the possibility of a function modifying the array that has been passed to it.

Because indexing expressions are translated into indirection expressions, all pointers (with the exception of void* ) can be indexed. Pointers can therefore be regarded as "arrays without bodies" and hence a body can be provided dynamically. A dynamic one-dimensional array is a pointer that points to a dynamically allocated segment to store the array items. Such arrays behave in all respects as static ones, but they can be extended or reduced if need be. Common errors associated with the use of dynamic arrays include (a) using the pointer without allocating the segment to store the array items (discussed in Chapter 3 as " uninitialized pointer" error), (b) allocating insufficient storage (discussed in Chapter 3 as "overflow" error), and (c) forgetting to deallocate unneeded dynamic arrays or unneeded segments that may lead to memory leaks.

Strings are treated in C/C++ as character arrays terminated by NULL . They can be stored in either statically or dynamically allocated arrays. In either case, a string is represented by a char* pointer that points to the beginning of the string, and the string spans the memory from its beginning to the first NULL . Even though strings are not an innate data type of C/C++ and the language provides no means to deal with them, standard functions (declared in the string.h header file) that come with any C/C++ compiler cover the whole range of necessary operations with strings including copying, duplication, concatenation, length, and so on.

In C++ the programmer is allowed to overload the index operator operator[] , which can thereby be defined for any class, and then treat objects of this class as arrays. These arrays may have all kind of properties; for example, they can dynamically grow and shrink as needed or can provide for run-time index range checking.

Категории