Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C function changes behaviour depending on whether it has a call to printf in it [closed]

Tags:

c

printf

I 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.


1 Answers

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.

like image 98
Pascal Cuoq Avatar answered Dec 05 '25 01:12

Pascal Cuoq



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!