Re: a[i] = a[j]... Dangerous?
- From: "Robbie Hatley" <see.my.signature@xxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 2 Jul 2008 16:34:08 -0700
<s0suk3@xxxxxxxxx> wrote:
I'm a bit unclear whether a statement such as 'a[i] = a[j];' causes
undefined behavior or some other abnormalities. A statement such as
'a[i] = a[i++];' would definitely cause problems because of the double
use and side effect of 'i'. But in the former case, will the double
use of the array 'a' cause problems? Or will the right operand
('a[j]') of the assignment be evaluated first and then safely assigned
to the right operand ('a[i]')?
"a[i] = a[j];" is legal.
The only surprises it might have in store is if you don't keep
the ranges of numbers over which i and j vary non-overlapping.
If they overlap, stuff will get over-written. If that's what
you intended, great; otherwise, it's a problem.
I use something like the above in production code at work,
to periodically move the right-most 80% of an array to the
far left, freeing up space on the right. A FIFO buffer;
only the most recent data is retained, due to space limitations:
int blat[800];
.... do some stuff ...
// shift data left when buffer gets full:
if (BufferIsFull())
{
for ( i = 0 ; i < 700 ; ++i )
{
// Discard oldest 100 bytes and shift newest 700 leftward,
// freeing 100 bytes on right side of array:
blat[i] = blat[i+100];
}
}
--
Cheers,
Robbie Hatley
lonewolf aatt well dott com
www dott well dott com slant user slant lonewolf slant
.
- Follow-Ups:
- Re: a[i] = a[j]... Dangerous?
- From: Kaz Kylheku
- Re: a[i] = a[j]... Dangerous?
- From: vippstar
- Re: a[i] = a[j]... Dangerous?
- References:
- a[i] = a[j]... Dangerous?
- From: s0suk3
- a[i] = a[j]... Dangerous?
- Prev by Date: Re: parsing a file error
- Next by Date: Re: a[i] = a[j]... Dangerous?
- Previous by thread: Re: a[i] = a[j]... Dangerous?
- Next by thread: Re: a[i] = a[j]... Dangerous?
- Index(es):
Relevant Pages
|