Bleamer
Bleamer

Reputation: 647

Negative array index in C

There have been other question/answers on negative array in C in the forum, but i would request the answer to these for a 32-bit compiler : If we have an array defined int test_array[5] = {1,2,3,4,5};

then what should following statements return test_array[20], test_array[-2], test_array[-32764], test_array[4294967700](value greater than what 32 bit can accommodate), *(a-32764) etc

Does the compiler force any fixed value to be returned in case index go beyond its declared range ?

Upvotes: 3

Views: 8079

Answers (3)

Alok Save
Alok Save

Reputation: 206518

Accessing an array beyond its bounds results in Undefined Behavior(UB).
An -ve index is not an valid index and results in Undefined Behavior.

An Undefined Bheavior means anything can happen literally, If you are lucky your program will crash and the problem gets detected, If you are unlucky the code works fine all along and all hell breaks loose some day.
So, always avoid writing any code which causes an Undefined Behaivor.

Does the compiler force any fixed value to be returned in case index go beyond its declared range ?

NO
The programmer has to take care of this. The Standard does not need the compiler to give you any indication/warning for this.The standard just defines it as an UB.

Furthermore the standard identifies all of the following scenarios to cause Undefined Behavior:

  • Addition or subtraction of a pointer into, or just beyond, an array object and an integer type produces a result that does not point into, or just beyond, the same array object.
  • Addition or subtraction of a pointer into, or just beyond, an array object and an integer type produces a result that points just beyond the array object and is used as the operand of a unary * operator that is evaluated.
  • An array subscript is out of range, even if an object is apparently accessible with the given subscript (as in the lvalue expression a[1][7] given the declaration int a[4][5]).

Upvotes: 6

David Heffernan
David Heffernan

Reputation: 612844

It is indefined behaviour as you write it since you are accessing the array out-of-bounds.

However, negative indices do not necessarily mean undefined behaviour. The following code is well defined:

int test_array[5] = {1,2,3,4,5};
int *p = test_array + 1;
int i = p[-1];//i now has the value 1

This is equivalent to:

int i = *(p-1);

Upvotes: 14

pmg
pmg

Reputation: 108978

Accessing elements outside the array is Undefined Behaviour.

Furthermore, making a pointer point to an element outside of the array, except for the (inexistant) one-past-the-last, is also Undefined Behaviour. Accessing the one-past-the-last is Undefined Behaviour (the pointer existence is ok)

int arr[42] = {0};
int *ptr = arr;
ptr += 41; /* ok, ptr points to the last element of arr */
*ptr;      /* ok */
ptr += 1;  /* ok, ptr points to one-past-the-last */
*ptr;      /* UB */
ptr += 1;  /* UB */

ptr = arr;
ptr -= 1;  /* UB */

Upvotes: 1

Related Questions