Summary

 < Day Day Up > 



This chapter introduced you to arrays, their purpose, and their use. An array is a contiguous allocation of memory to homogeneous objects. Contiguous means the objects are stored one after another in memory, and homogeneous means the objects in the array are all the same type. Arrays can contain user-defined data types as well as fundamental data types like int or float. They can also contain pointers to these types and function pointers. Arrays of user-defined data types are covered in subsequent chapters.

An array begins at a certain memory address. A static array name is a const pointer. For statically allocated arrays of objects the array name points to the first element. Each successive object in an array is located one allocation unit from the previous object. The size of the allocation unit is determined by the type of objects an array is declared to contain.

There are two ways to access array elements: array subscripting using the [ ] operator and an index value, or via pointer arithmetic using the pointer dereference operator *. Thus, array_name[0] will yield the same element as *(array_name).

Beware the uninitialized array! Use an initializer list to set the values of an array at declaration.

Multi-dimensional arrays are arrays of arrays. Multi-dimensional arrays are stored in memory in row major order. Using named constants or variables in array declarations can help clarify the intended use of each dimension. Remember the pattern: smaller dimensional units are displaced to the right in array declarations.

A dynamic array is one that has been created in the application heap using the new[] operator. Do not forget to release dynamically allocated arrays using the delete[] operator or you will suffer memory leaks. Also, if you have an array of pointers to object, do not forget to release the memory for each dynamically created object using the delete operator before releasing the array.

Strings are arrays of characters terminated with a null character ‘\0’. Allow for one extra element in the sizing of any array in which you intend to hold or manipulate strings.



 < Day Day Up > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net