Node:Array bounds, Next:, Previous:Arrays, Up:Arrays



Array bounds

In keeping with C's free-wheeling, "I assume you know what you're doing" policy, the compiler does not complain if you try to write to elements of an array that do not exist. For example, the code below defines an array with five elements. (Remember, C arrays are zero-based.)

char my_array[4];

Given the line of code below, your program will happily try to write the character * at location 10000. Unfortunately, as may happen when writing to an uninitialized pointer, this may crash the program, but will probably do nothing worse on a GNU system. (See Pointers and initialization.)

my_array[10000] = '*';

The first and last positions in an array are called its bounds. Remember that the bounds of an array are zero and the integer that equals the number of elements it contains, minus one.

Although C will not warn you at compile-time when you exceed the bounds of an array, the debugger can tell you at run-time. See Introduction to GDB, for more information.