This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug middle-end/48001] inconsistent warning within loop always taken and outside the loop
- From: "jerome.borme at gmail dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Sat, 5 Mar 2011 22:25:33 +0000
- Subject: [Bug middle-end/48001] inconsistent warning within loop always taken and outside the loop
- Auto-submitted: auto-generated
- References: <bug-48001-4@http.gcc.gnu.org/bugzilla/>
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48001
--- Comment #3 from jerome.borme at gmail dot com 2011-03-05 22:25:23 UTC ---
Here is a more complete part of a program where I found the problem. The
following code will allocates 3 vectors fx, fy, fz with specific length, then
call a function to display the averaged value of the fx, fy and fz, and the
averaged value of sqrt (fx^2+fy^2+fz^2). In the even more complete version,
maximum and minimum values of fx, fy, fz and sqrt (...) are also computed.
After calculation, values are displayed.
The initialization of sum_f has been forgotten, but no warning is issued,
although sum_f is used in the loop.
(Actually for some reason when I run this test, sum_f seems to have 0 even if
it is not written there, but I believe this is coincidence, because in the real
calculation code the displayed value was wrong.)
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define SIZE 100
void average (int nmax, double *fx, double *fy, double *fz) {
double sum_fx = 0, sum_fy = 0, sum_fz = 0, cur_f = 0, sum_f ;
long int i ;
for (i = 0 ; i < nmax ; i++) {
sum_fx += fx[i] ;
sum_fy += fy[i] ;
sum_fz += fz[i] ;
cur_f = pow ((fx[i] * fx[i]) + (fy[i] * fy[i]) + (fz[i] * fz[i]), 0.5) ;
sum_f += cur_f ;
}
printf ("# Average field : %g %g %g %g\n", sum_fx/nmax, sum_fy/nmax,
sum_fz/nmax, sum_f/nmax) ;
}
int main () {
long int i ;
double *fx, *fy, *fz ;
fx = (double *) malloc (SIZE * sizeof (double)) ;
fy = (double *) malloc (SIZE * sizeof (double)) ;
fz = (double *) malloc (SIZE * sizeof (double)) ;
for (i = 0 ; i < SIZE ; i++) {
fx[i] = 1 ;
fy[i] = 1 ;
fz[i] = 1 ;
}
average (SIZE, fx, fy, fz) ;
return 0 ;
}