C++ Arrays and Memory Allocation: Basics and Dimensions - Prof. Donna Pompei, Study notes of Computer Science

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

Pre 2010

Uploaded on 08/17/2009

koofers-user-ck9-1
koofers-user-ck9-1 🇺🇸

10 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
APPENDIX A
Arrays, memory and Allocation Basics
Carrano, Helman, Veroff, pp. A26-A32
A. Arrays
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.
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download C++ Arrays and Memory Allocation: Basics and Dimensions - Prof. Donna Pompei and more Study notes Computer Science in PDF only on Docsity!

APPENDIX A

Arrays, memory and Allocation Basics

Carrano, Helman, Veroff, pp. A26-A

A. Arrays

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:

  1. Random Access Memory (RAM) fast and volatile
  2. Disk slow (relatively speaking) and permanent Typical Memory Organization:  Bytes are groups of 8 bits.  A Byte can store 2 8 = 256 different values.  Bytes are indexed by address.

Storage Allocation Consequences

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.

Storing and Accessing an Array

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.

Accessing Nested Components of Arrays

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

Arrays with Multiple Dimensions

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

C. Adding Flexibility to Arrays in C++

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);