What does the standard say about array access wraparound?
From: David Mathog (mathog_at_caltech.edu)
Date: 05/27/04
- Next message: David Schwartz: "Re: [OT] Re: writing a dailer in c for a 8051 based system"
- Previous message: CBFalconer: "Re: program output?"
- Next in thread: Mike Wahler: "Re: What does the standard say about array access wraparound?"
- Reply: Mike Wahler: "Re: What does the standard say about array access wraparound?"
- Reply: Dan Pop: "Re: What does the standard say about array access wraparound?"
- Reply: Default User: "Re: What does the standard say about array access wraparound?"
- Reply: Stephen L.: "Re: What does the standard say about array access wraparound?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 27 May 2004 08:02:56 -0700
If this:
int i,sum;
int *array;
for(sum=0, i=0; i<len; i++){
sum += array[i];
}
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[i];
}
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
mathog@caltech.edu
Manager, Sequence Analysis Facility, Biology Division, Caltech
- Next message: David Schwartz: "Re: [OT] Re: writing a dailer in c for a 8051 based system"
- Previous message: CBFalconer: "Re: program output?"
- Next in thread: Mike Wahler: "Re: What does the standard say about array access wraparound?"
- Reply: Mike Wahler: "Re: What does the standard say about array access wraparound?"
- Reply: Dan Pop: "Re: What does the standard say about array access wraparound?"
- Reply: Default User: "Re: What does the standard say about array access wraparound?"
- Reply: Stephen L.: "Re: What does the standard say about array access wraparound?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|