public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* strange problem with "plus-equals"
@ 2006-12-31  4:23 David Mebane
  2006-12-31 12:38 ` Andrew Haley
  0 siblings, 1 reply; 2+ messages in thread
From: David Mebane @ 2006-12-31  4:23 UTC (permalink / raw)
  To: gcc-help


Hello,

Having a problem with "+=" operator (and the addition / assignment operation in
general) in g++.

I have a protected function that looks like:

double LSMbdnew::norm(double *vec1, long num)	{
	long i;
	double a, ret;
	ret = 0.0;
	for (i=0; i<num; i++)	{
		a = *(vec1 + i);
		ret = ret + a*a;
	}
	return ret;
}

Problem is, the variable ret becomes zero after the second iteration in the test
I'm running.  How does this happen?  I have checked the variable ret -- there is
no conflict in this scope.  In fact, if I change the last line in the loop to a
straight assignment, there's no problem.  But when I try to use assign/add,
problem.

Thanks,
-David

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: strange problem with "plus-equals"
  2006-12-31  4:23 strange problem with "plus-equals" David Mebane
@ 2006-12-31 12:38 ` Andrew Haley
  0 siblings, 0 replies; 2+ messages in thread
From: Andrew Haley @ 2006-12-31 12:38 UTC (permalink / raw)
  To: David Mebane; +Cc: gcc-help

David Mebane writes:
 > 
 > Having a problem with "+=" operator (and the addition / assignment operation in
 > general) in g++.
 > 
 > I have a protected function that looks like:
 > 
 > double LSMbdnew::norm(double *vec1, long num)	{
 > 	long i;
 > 	double a, ret;
 > 	ret = 0.0;
 > 	for (i=0; i<num; i++)	{
 > 		a = *(vec1 + i);
 > 		ret = ret + a*a;
 > 	}
 > 	return ret;
 > }
 > 
 > Problem is, the variable ret becomes zero after the second iteration in the test
 > I'm running.  How does this happen?  I have checked the variable ret -- there is
 > no conflict in this scope.  In fact, if I change the last line in the loop to a
 > straight assignment, there's no problem.  But when I try to use assign/add,
 > problem.

If you turn this into a test case people can actually run, then people
will help you.  A naked function isn't a program that people can test.

Andrew.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2006-12-31 12:38 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-12-31  4:23 strange problem with "plus-equals" David Mebane
2006-12-31 12:38 ` Andrew Haley

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).