






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to arrays and memory allocation in c++. It covers the basics of arrays, including their definition, declaration, and indexing. The document also explains the concept of memory organization, the storage of values, and the consequences of storage allocation. Additionally, it discusses how to compute the location of array elements and access nested components of arrays. The document concludes with a brief explanation of arrays with multiple dimensions and adding flexibility to arrays in c++.
Typology: Study notes
1 / 12
This page cannot be seen from the preview
Don't miss anything!







Carrano, Helman, Veroff, pp. A26-A
Defn : Arrays are indexed collections of components all of the same type. When using an array in C++, the dimensions and type of elements stored in the array must be declared. The dimensions of the array will always begin with 0. Example: const int CALENDAR = 12; double Income[CALENDAR]; This declaration will create an array called Income made up of twelve variables of type double. The indices of the array will be 0 to 11.
B. Memory and Allocation Basics Typical storage types for computers:
The size of the storage space limits the quantities that can be stored. Example: For INTEGERS: 1 Byte -128..127, since 2 8 = 2 Bytes -32,768..32767, since 2 16 = 4 Bytes -2,147,483,648.. 2,147,483,637, since 2 32 =4,294,967, For FLOATS: The more Bytes available, the more digits that can be stored after the decimal point.
typedef int studentScores[4]; studentScores X; X[0] X[1] X[2] X[3] 424 425 426 427 428 429 430 431 To compute the location of X[j]: 424 + (j - 0) * 2 In general, the formula for an array A is: &A[j] = Starting Address + (j - 1 st Index) * Component Size The compiler calculates this arithmetic formula so that the elements of the array can be easily accessed.
typedef int studentScores[4]; typedef studentScores classScores[100]; classScores mathClass; If integers are each 2 Bytes, then the size of each component of mathClass is: (4 * 2) = 8 = the full size of studentScores To find mathClass[j]: &mathClass[j] = Starting Address + (j - 1st Index of classScores) * 8 To find &mathClass[j][k]: &mathClass[j][k] = &mathClass[j] + (k - 1st Index of studentScores) * Component size of studentScores = Starting address + (j - 0) * 8 + (k - 0) * 2
In storing a 2-dimensional array (matrix), either the rows or columns can be grouped together. For simplicity, we will assume the rows are grouped together in row-major order. Example: typedef float Matrix[4][3]; Matrix Z; In row-major order , the rows are stored together. The elements are grouped by the 1 st index. Z[0][-] Z[1][-] Z[2][-] Z[3][-] 100 111 112 123 124 135 136 147
Defn: An unconstrained array is an array in which the bounds of the array, and the indices of the array, are not specified when the type is declared. Unlike Ada, the C++ language does not have built-in unconstrained arrays. In C++, the indices of arrays must be declared, and all arrays begin with index 0. However, a programmer can create an array class that permits the equivalent of Ada's unconstrained arrays and beginning index value.
Example: A vector class for one-dimensional integer arrays: class vector { public: // vector constructor -- last_index = // first_index + vector_size - 1 // space is allocated dynamically vector(int vector_size, int first_index = 0); // … private: int * array; int size; int first; int last; }; To instantiate two one-dimensional arrays, one consisting of 5 elements beginning with index 0 and the other consisting of 10 elements beginning with index 1: vector array_A(5); vector array_B(10, 1);