Results 1 to 4 of 4
Hi!
I have this very unusual problem. Basically, my program works differently if I use an inocent printf. The piece of offending code is this:
Code:
float dist (int i, ...
- 05-23-2009 #1Just Joined!
- Join Date
- May 2009
- Posts
- 2
Weird behaviour
Hi!
I have this very unusual problem. Basically, my program works differently if I use an inocent printf. The piece of offending code is this:
Code:float dist (int i, float a, float b) { return (b-a) * dw[i]; } float Dist(float *A, float *B) { float d, D; for(int i=0;i<NFeatures;i++) { d = dist(i, A[i], B[i]); D += d*d; //printf("...\n"); assert(d<=1E20); //printf("--- %f %f %f\n",d,D,B[i]); } printf("--- %f %f\n",d,D); return sqrt(D); } void ClusterExamples(int & nc, int * ec, float ** Clust) { int i,j,mini; float min, dist; nc = (int) (sqrt(Ex.size())); float* csize = (float*)malloc(nc*sizeof(float)); Clust = (float**)malloc(nc*sizeof(float*)); ec = (int*) malloc(Ex.size()*sizeof(int)); for(i=0;i<nc;i++) { Clust[i] = (float*) malloc(NFeatures * sizeof(float)); for(j=0;j<NFeatures;j++) Clust[i][j] = Ex[i].fv[j]; } // initialized everything, including random initial clusters while (1) // i donnow what { printf("sdfasdfasdf\n"); // reset examples' clusters for(i=0;i<Ex.size();i++) { min = MAXDIST; mini = -1; for(j=0;j<nc;j++) { dist = Dist(Ex[i].fv, Clust[j]); printf("]]] %d %f %f\n",mini, dist,min); if (dist < min) { min = dist; mini=j;} } ec[i]=mini; //printf("========%d %f\n",i, min); } // now recalculate clusters for(i=0;i<nc;i++) { for(j=0;j<NFeatures;j++) Clust[i][j]=0; csize[i]=0; } for(i=0;i<Ex.size();i++) { int c = ec[i]; for(j=0;j<NFeatures;j++) Clust[c][j]+=Ex[i].fv[j]; csize[c]++; //printf("%d %d %d %0.0f\n",nc,c, i, csize[c]); } for(i=0;i<nc;i++) for(j=0;j<NFeatures;j++) { assert(csize[i]!=0); Clust[i][j]/=csize[i]; } } }
With this code, the program fails at the bolded assertion.
But if I uncomment the printf right after (or before) the assertion, the program does not stop. The printfs print and everything is nice. The third printf ( the one with "]]]" prints nice floats).
On the other hand, without the assertion, and with the two printfs commented, the thid one shows that dist is "nan".
So, first I noticed that dist is nan, but when I put the printfs in the Dist() function, suprise, no more nan. Frankly I'm really amazed.
I usually search for stuff like this on the net and don't bother people
. But this was just too weird for me to come up with relevant search terms.
Any help would be really appreciated.
- 05-24-2009 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
You don't show where a number of variables (globals) are defined, initialized, and/or utilized, such as Ex (class of what?) and Clust (you showed it getting initialized, but not where it is declared and passed to ClusterExamples(). So, basically there isn't enough information here to go on. I could make a number of criticisms of this code, but essentially it should do what you want if all the values and array indexes are valid. The fact that dist is showing up as NAN means that the value it was initialized with was a NAN, so the culprit is probably the Ex array. The fact that adding/removing the printf() statements changes the behavior of the probram is typical of a situation where you have munged your stack or heap (probably stack), so the conclusion that has to be reached is that something is not initialized correctly or an array was overrun. FWIW, a bounds-checking tool such as IBM/Rational Purify will find the cause of this sort of problem PDQ.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 05-24-2009 #3Just Joined!
- Join Date
- May 2009
- Posts
- 2
Thanks Rubberman! You suspected right, there was a problem with initialization.
I fixed it.
- 05-24-2009 #4Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Glad to help point you in the right direction. Formal root-cause analysis techniques are very useful for debugging software like this. It helps you postulate and frame the problem in a logical and coherent manner, reducing the overall time to find and fix bugs in complex applications. Experience helps, also!
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote
