answersLogoWhite

0


Best Answer

We don't use the term "memory cells", we use the term "data elements", or simply "elements". A memory cell usually means 1 bit of memory whereas an array element occupies an arbitrary number of bits dependant upon the size of the element and the architecture. You can access the individual bits of an element, of course, but you cannot use the subscript operator for that -- it will only access the element itself, it's up to you to access the bit within that element.

To access any array element using the subscript operator, [], you need two pieces of information: the start address of the array and the zero-based offset of the element. Since every element in an array is exactly the same length (in bytes), you simply multiply the offset by the element size and add this product to the start address. The result of this "pointer arithmetic" is the start address of the element you're looking for.

The subscript operator simplifies this process because it is not necessary for you to know the size of an element. All you need is the offset and the start address. The start address is provided for you automatically by the array name itself. That is, the array name is a reference to the start address of the array.

The subscript operator also requires a zero-based offset, thus for an array of n elements, the offset must be in the closed range 0 to n-1. If the offset is outwith this range, you will end up accessing memory that does not belong to the array and will result in undefined behaviour.

Since it is zero-based, the offset will always be "off-by-one" with respect to an element's position. That is, the 5th element can be found at offset 4 and the 1st at offset 0. The last element is always at n-1 for an array of n elements.

For an array of type int named A, we can print the 5th element using pointer arithmetic as follows:

std::cout << *(sizeof (int) * 4 + A);

Using the subscript operator we can achieve the same thing with:

std::cout << A[4];

Note that the subscript operator is nothing more than syntactic sugar-coating. The pointer arithmetic is still going on behind the scenes, it's merely simplifying our code, making it easier to read. There is no difference in terms of performance.

Note also that the above code will only work if array A has 5 or more elements. If it has fewer than 5, our code has undefined behaviour, which is never a good thing. Our code may appear to work but it is inherently dangerous to release a program with undefined behaviour. The program may inexplicably crash for no apparent reason, vital data could be wiped from hard-drives or people could die. With undefined behaviour anything is possible.

If A were a vector rather than a C-style array, we would eliminate the risk of undefined behaviour when using subscripts because an out-of-range subscript would instantly result in an exception being thrown. That immediately tells us there's a fundamental problem and we can immediately set about fixing it.

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is used in conjunction with the subscript operator to access the memory cells in an array?
Write your answer...
Submit
Still have questions?
magnify glass
imp