There is no way around this.
You are operating at the limits of your array and you have to make sure you do not work outside of the memory portion where your elements are.
Here are some code snippets that should illustrate the principal connection.
int i;
int a[ARR_MAX]; //a[0] to a[ARR_MAX-1]
for(i = 0; i < ARR_MAX; ++i)
{
a[i-1] //is an error if i is 0
a[i] //the usual way to access an array
a[i+1] //is an error if i is (ARR_MAX-1)
}
for(i = 1; i < (ARR_MAX-1); ++i)
{
a[i-1] //i does not start a 0
a[i+1] //i is never (ARR_MAX-1)
}
for(i = 0; i < ARR_MAX; ++i)
{
a[(i-1)%ARR_MAX] //is an error if i is 0
a[i%ARR_MAX] //is irrelevant in this loop
a[(i+1)%ARR_MAX] //is a[0] if i is (ARR_MAX-1)
}
for(i = 1; 1; ++i) // loops forever, i will get negative at some point
{
a[i%ARR_MAX]//can never be an error, unless i is negative
a[(i+n)%ARR_MAX]//can never be an error, unless i is negative
a[(i-n)%ARR_MAX]//is an error for n > i
}
Applied to your case it may simply be correct to us start and stop the loop one element earlier.
I will refine this statement if you can give some details about your usecase.
variable
?? Is it a real variable, or a function? – ikh Aug 13 '14 at 11:29arr[arr.size]
witharr[array.size-1]
andarr[0]
) – Dimitri Mockelyn Aug 13 '14 at 11:30