D
David Mathog
If this:
int i,sum;
int *array;
for(sum=0, i=0; i<len; i++){
sum += array;
}
is converted to this (never mind why for the moment):
int i,sum;
int *array;
int *arrl;
arl=&array[-len];
for(sum=0,i=len; i<2*len; i++){
sum += arrl;
}
it should give the same result. But there are some funny
things that can happen. For instance, if &array is 1000 and
len is 100000. In that case arrl will hold an address
(1000-100000) which presumably wraps around since the
pointer should be an unsigned int (whatever size int is).
The address it points to will be MAX_POINTER - 100000 + 1000.
When the second form loop loop begins i=len (100000) so
arrl[100000] will wrap back around and point to the same
place as array[0].
Or will it?
It seems possible that this sort of array access "off the top of
memory" could trigger a fault.
What does the C standard say about this (if anything)?
Thanks,
David Mathog
(e-mail address removed)
Manager, Sequence Analysis Facility, Biology Division, Caltech
int i,sum;
int *array;
for(sum=0, i=0; i<len; i++){
sum += array;
}
is converted to this (never mind why for the moment):
int i,sum;
int *array;
int *arrl;
arl=&array[-len];
for(sum=0,i=len; i<2*len; i++){
sum += arrl;
}
it should give the same result. But there are some funny
things that can happen. For instance, if &array is 1000 and
len is 100000. In that case arrl will hold an address
(1000-100000) which presumably wraps around since the
pointer should be an unsigned int (whatever size int is).
The address it points to will be MAX_POINTER - 100000 + 1000.
When the second form loop loop begins i=len (100000) so
arrl[100000] will wrap back around and point to the same
place as array[0].
Or will it?
It seems possible that this sort of array access "off the top of
memory" could trigger a fault.
What does the C standard say about this (if anything)?
Thanks,
David Mathog
(e-mail address removed)
Manager, Sequence Analysis Facility, Biology Division, Caltech