UMBC CS 201, Fall 06
Fall '06

CSEE | 201 | 201 F'06 | lectures | news | help

Internal Representation
of Arrays in C


Fully understanding how arrays work in C requires some understanding of how they are represented in the computer's memory.

In C, an array will be implemented as a single block of memory, with element 0 occupying the first "slot" in that block. As a result, C subscripts are closely related to actual memory addresses, and to the notion of "pointer".

Possible dire consequences from the misuse of arrays should motivate you to pay close attention to the array indices.

Bits, bytes and words

Beyond words, we speak of of memory.

Memory addresses

Each memory location in the computer is identified by a unique numeric address.

Typically, these are integers which begin with 0 for the first byte in memory and count upward from there.

Some data elements need more than one byte to store.

For example, an integer on an SGI computer might be encoded using four consecutive bytes and a double using eight bytes.

Such data values are indexed by the location of the first byte of their encoding.

The sizeof operator

    sizeof(type) returns the number of bytes needed
                 to represent a value of that type.

Allocation of memory to variables

When you declare a variable, the complier allocates memory locations in which to place its value. For a simple variable (e.g., int, double) of type X the compiler allocates sizeof(X) bytes to hold it.

For an array of length L and type X , the compiler allocates L*sizeof(X) bytes.

Given an array like int scores[100] , the compiler allocates 400 bytes starting at some location, say 64789.

Given an expression like scores[5] the compiler accesses the value stored at the memory location starting with byte 64789+(5*4).

In general, foo[i] is located at byte:

 base address of foo  +  i * sizeof (type of array)

References to elements outside of the array bounds

C will not flag as an error a reference past the end of the array.

scores[9999] will try to access some random words in memory.

Making reference to elements outside of the array bounds is one of a few very common causes of sementation faults and core dumps.

CSEE | 201 | 201 F'06 | lectures | news | help

Tuesday, 22-Aug-2006 07:13:55 EDT