answersLogoWhite

0

An array is any contiguous block of allocated memory that contains one or more instances of the same data type, where each instance (known as an element) is accessible independently of the others via the element's unique index (calculated as a zero-based offset from the start of the array).

Arrays are typically one-dimensional (a string of characters is an example of a one-dimensional array) but they can be multi-dimensional. This allows data to be conceptualised as a rectangular grid, with rows and columns (a two-dimensional array), or as a cuboid (a three-dimensional array).

Going beyond three-dimensions is difficult to imagine in a 3D world, however a four-dimensional array can be likened to a cuboid where each element in the cuboid contains a one-dimensional array. A six-dimensional array is therefore a cuboid where each element contains a cuboid, and so on.

Arrays can be static or dynamic. Static arrays are those where the dimensions are known at compile time and are allocated on the stack. Dynamic arrays are dimensioned at runtime and allocated on the heap.

The following are examples of static arrays:

char x[10]; // Allocates memory for 10 characters.

int sudoku[9][9]; // Allocates memory for 81 integers (9x9).

int rubik[3][3][3]; Allocates memory for 27 integers (3x3x3).

It's important to remember that dimension indices are zero-based, because they represent memory offsets from the beginning of the array. Therefore the first element of array x is x[0], and the last is x[9]. x itself is an alias to the block of memory allocated to the array named x, and is therefore the same as the address occupied by element x[0].

Offsets are calculated by the size of the data in each element, thus x[1] can be found at the address 1*sizeof(char) bytes from the address of x. In multi-dimensional arrays, the offsets are calculated by multiplying the dimensions. Thus rubik[2][0][1] will be found ((2*9)+(0*3)+1)*sizeof(int) bytes from the address of rubik.

Dynamic arrays are created by maintaining a pointer to the start of the array. Thus:

int * pArray=new int[10]; // Allocates memory to 10 integers on the free store.

The value stored in the variable pArray is therefore the start address of the array, the same address occupied by pArray[0].

The memory can be released with a call to delete:

delete[] pArray;

Note the use of [] to indicate that the entire array is to be deleted. If we don't do this, only the first element would be released, causing a memory leak.

Multi-dimensional dynamic arrays are a bit trickier to deal with as each dimension adds an extra level of indirection and the array itself is not guaranteed to reside in contiguous memory. In fact, it's easier to think of them as being separate one-dimensional arrays, each with their own pointer, and those pointers residing in their own separate array.

However, it's still possible to allocate multi-dimensional arrays in contiguous memory by allocating the memory as a single block, rather than allocating each dimension independently. A separate array of pointers are used to divide the block however you see fit. For instance, to allocate a 5x6 dynamic array of type int, you'd use the following:

int ** pp = new int*[5]; // allocate pointer array

pp[0] = new int[5*6]; // allocate the contiguous block

for(int i=1;i<5;++i) pp[i]=pp[i-1]+6; // initialise the pointer array to divide the block into rows.

Thereafter, you can access the array as normal, thus pp[3][2] references the integer in the fourth row and third column of the contiguous block (remember, indices are always zero-based).

The array must be destroyed in the reverse order of creation:

delete[] pp[0]; // release the contiguous block

delete[] pp; // release the pointer array

User Avatar

Wiki User

13y ago

What else can I help you with?

Related Questions

What does square brackets mean?

It means indexing into an array. The array could be an array of built in primitive types or array of objects. The index must be a numeric value greater than or equal to 0.


Create an anonymous array in java?

An anonymous array in Java is just an array with its contents declared at instantiation. Normal array declaration: int[] nums = new int[3]; nums[0] = 0; nums[1] = 1; nums[2] = 2; Anonymous array declaration: int[] nums = new int[] {0,1,2}; The two examples above will each result in the same array.


What does PGA mean in computing?

PGA Pin Grid Array is the socket that holds the CPU, there is also the LGA/ land grid array


Synonym for array?

Batch, bunch, bundle, cluster, design, display, lineup, pattern, or supply. Those words mean array.


What happens if an array overrun occurs in java?

If you mean that you try to access an index outside of the bounds of the array, then it will result in an IndexOutOfBoundsException being thrown.


What does SVGA mean in electronics subjects?

It is a computer graphics standard. VGA = Video Graphics Array SVGA = Super Video Graphics Array.


What does it mean for a subscript to be out-of -bounds?

This is a type of error that usually occurs in computer programs. An array is defined in which the elements of the array are identified by one or more subscripts. Suppose you have an array which is declared to be of dimension 23. Then if the program tries to access element 26 in that array, it cannot because there is no element of the array in that position. That is when you will get this error message.


What does ega mean?

Computer term for Extended Graphics Array(EGA)


What does the acronym GCRMA mean?

GC robust multi-array average


Is it necessary to read or display the elements of an array in order of its subscripts?

By no means; you can access any random array element. If you have ever seen examples which process them in order, it is because of the following: when the order doesn't matter (for example, you want to calculate the sum of all the array elements), it is easiest to process them in order.


What does it mean when use percent 17 or mod any number in Array in C programming?

I mean %17


What are the rule used in naming a variable Give examples?

variable is a character or number sequence of a character is called array name