By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,137 Members | 1,024 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,137 IT Pros & Developers. It's quick & easy.

problem with multiple ++, --

P: n/a

Hello

Does anyone know why the following loops don't give the same results

for(k=0; k<size; k+=2){
*pSum+=((*pa1++)+(*pb2--))*((*pa1++)+(*pb2++));
pb2++;
}

and

for(k=0; k<size; k+=2)
*pSum+=(pa1[k]+pb2[k])*(pa2[k]+pb1[k]);
where we start with
pa2 = pa1+1;
pb2 = pb1+1;

k is even and we are definitely not stepping out of bounds

Is this a compiler error or an error on my part?

The first loop is a little faster but unfortunately gives the wrong results.
(Can anyone suggest a faster way?)

Thanks
Jeff
Dec 20 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Jeff wrote:
Hello

Does anyone know why the following loops don't give the same results

for(k=0; k<size; k+=2){
*pSum+=((*pa1++)+(*pb2--))*((*pa1++)+(*pb2++));
pb2++;
}
Please take a look at http://c-faq.com/expr/seqpoints.html

Dec 20 '06 #2

P: n/a
Jeff wrote:
Does anyone know why the following loops don't give the same results

for(k=0; k<size; k+=2){
*pSum+=((*pa1++)+(*pb2--))*((*pa1++)+(*pb2++));
pb2++;
}
This loop will invoke undefined behavior - anything can happen,
including your computer crashing as soon as you try to execute this
code.

Even if it wasn't undefined behavior, the order in which subexpressions
are evaluated is completely unspecified, so you wouldn't know which one
of pa1 [0] and pa1 [1] would be used in the left product and in the
right product, so you couldn't expect any reasonable results anyway.

Dec 20 '06 #3

P: n/a

Jeff wrote:
Hello

Does anyone know why the following loops don't give the same results

for(k=0; k<size; k+=2){
*pSum+=((*pa1++)+(*pb2--))*((*pa1++)+(*pb2++));
This line invokes undefined behavior because you are trying to modify
an object more than once between sequence points. Since the behavior
is explicitly undefined, there is no correct result.

See the following:

http://www.c-faq.com/expr/evalorder1.html
http://www.c-faq.com/expr/evalorder2.html

You're relying on two mistaken assumptions: first, that the expressions
are evaluated in the order that they are written, and second, that the
side effects of the ++ and -- operators are applied immediately after
each expression has been evaluated.

There is no guarantee that *pb2-- is evaluated before *pb2++, nor is
there a guarantee that the side effect of either expression is applied
before the other one is evaluated.
pb2++;
}

and

for(k=0; k<size; k+=2)
*pSum+=(pa1[k]+pb2[k])*(pa2[k]+pb1[k]);
where we start with
pa2 = pa1+1;
pb2 = pb1+1;

k is even and we are definitely not stepping out of bounds

Is this a compiler error or an error on my part?

The first loop is a little faster but unfortunately gives the wrong results.
(Can anyone suggest a faster way?)

Thanks
Jeff
Dec 20 '06 #4

P: n/a
Jeff wrote:
Hello

Does anyone know why the following loops don't give the same results

for(k=0; k<size; k+=2){
*pSum+=((*pa1++)+(*pb2--))*((*pa1++)+(*pb2++));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Oh, Puh-leeze! Can't you check the FAQ before posting. Or you could
check your basic C text on sequence points. Or any of the hundreds of
threads (check the google groups archive) started by other clueless people.
Dec 20 '06 #5

P: n/a
Many thanks for the kind replies - and I hope Mr Ambuhl's headache is soon
better ;-)

Jeff
Dec 20 '06 #6

P: n/a
Jeff wrote:
Many thanks for the kind replies - and I hope Mr Ambuhl's headache is soon
better ;-)
It's not my headache that needs getting better: it's your manners. A
civilized person knows he should check the FAQ, follow the newsgroup,
and check the archives before posting a question. Any one of those
would have kept you from being an obnoxious boor.
Dec 21 '06 #7

P: n/a
In article <11**********************@i12g2000cwa.googlegroups .com>,
John Bode <jo*******@my-deja.comwrote:
>
Jeff wrote:
>Hello

Does anyone know why the following loops don't give the same results

for(k=0; k<size; k+=2){
*pSum+=((*pa1++)+(*pb2--))*((*pa1++)+(*pb2++));

This line invokes undefined behavior because you are trying to modify
an object more than once between sequence points. Since the behavior
is explicitly undefined, there is no correct result.
The dogmatically correct statement is that *all* results are correct.

Dec 21 '06 #8

P: n/a
Martin Ambuhl wrote:
Jeff wrote:
>Many thanks for the kind replies - and I hope Mr Ambuhl's headache
is soon better ;-)

It's not my headache that needs getting better: it's your manners.
A civilized person knows he should check the FAQ, follow the
newsgroup, and check the archives before posting a question. Any
one of those would have kept you from being an obnoxious boor.
You omitted including adequate quotes, and refraining from
top-posting. However, offhand Jeff appears to be educable.
Carrots might produce better results than sticks.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Dec 21 '06 #9

P: n/a
Jeff wrote:
Hello

Does anyone know why the following loops don't give the same results

for(k=0; k<size; k+=2){
*pSum+=((*pa1++)+(*pb2--))*((*pa1++)+(*pb2++));
pb2++;
}

and

for(k=0; k<size; k+=2)
*pSum+=(pa1[k]+pb2[k])*(pa2[k]+pb1[k]);
where we start with
pa2 = pa1+1;
pb2 = pb1+1;

k is even and we are definitely not stepping out of bounds

Is this a compiler error or an error on my part?

The first loop is a little faster but unfortunately gives the wrong results.
(Can anyone suggest a faster way?)

Thanks
Jeff

Welcome to c.l.c, Jeff. If you decide to stick around, you will
learn who are helpful and who are, well, not helpful. I had been
writing C code (as a hobby) for nearly a year before I ever heard
of sequence points, so it would have never occurred to me to
check the FAQ. Nevertheless, I think I'm still reasonably civilized.

JS
Dec 21 '06 #10

P: n/a

"John Smith" <JS****@mail.netwrote in message
news:WYxih.509031$5R2.400846@pd7urf3no...
Jeff wrote:
Welcome to c.l.c, Jeff. If you decide to stick around, you will learn who
are helpful and who are, well, not helpful. I had been writing C code (as
a hobby) for nearly a year before I ever heard of sequence points, so it
would have never occurred to me to check the FAQ. Nevertheless, I think
I'm still reasonably civilized.
Thanks John.

It seemed a reasonable question to me too - else I wouldn't have asked it.
I guess some people just have very sensitive toes :-)

Enough already.

Jeff
Dec 22 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.