C function changes behaviour depending on whether it has a call to printf in it [closed]
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this questionI have a function that processes some data and finds the threshold that classifies the data with the lowest error. It looks like this:
void find_threshold(FeatureVal* fvals, sampledata* data, unsigned int num_samples, double* thresh, double* err, int* pol) {
//code to calculate minThresh, minErr, minPol omitted
printf("minThresh: %f, minErr: %f, minPol: %d\n", minThresh, minErr, minPol);
*thresh = minThresh;
*err = minErr;
*pol = minPol;
}
Then in my test file I have this:
void test_find_threshold() {
//code to set up test data omitted
find_threshold(fvals, sdata, 6, &thresh, &err, &pol);
printf("Expected 5 got %f\n", thresh);
assert(eq(thresh, 5.0));
printf("Expected 1 got %d\n", pol);
assert(pol == 1);
printf("Expected 0 got %f\n", err);
assert(eq(err, 0.0));
}
This runs and the test passes with the following output:
minThresh: 5.000000, minErr: 0.000000, minPol: 1
Expected 5 got 5.000000
Expected 1 got 1
Expected 0 got 0.000000
However if I remove the call to printf() from find_threshold, suddenly the test fails! Commenting out the asserts so that I can see what gets returned, the output is:
Expected 5 got -15.000000
Expected 1 got -1
Expected 0 got 0.333333
I cannot make any sense of this whatsoever.
printf
can call malloc
. Because of this, if you have some dangling pointers, calling printf
can change the values pointed to by these. If your program was strictly conforming you wouldn't observe this kind of differences when calling printf
though (as you rightly expect). At worst an allocation in printf
could fail, but not silently corrupt other variables.
I would suspect some problem with memory access / allocation and test with valgrind
精彩评论