简体   繁体   中英

Wrong bool value passed to function in C++

I have a function defined in the header file prog.h, which takes a couple of arguments of the type bool, string and double.

string createDataFolder(bool setPBC, string distribution, double timestep, double simtime, double potRange, double potStrength, 
    double particlesize, bool steric, bool ranRod, bool ranU, bool rand, double dvar, double polydiam, bool Pointq){
    //...
    if (Pointq) folder += "/pointq";
    //....
    return folder;
}

When I call the function from inside my main() function in prog.cpp via

string folder = createDataFolder(setPBC, distribution, timestep, simtime, urange, ustrength, 
     particlesize, includeSteric, ranRod, ranU, rand, dvar, polydiam, Pointq);

the bool parameter Pointq is always passed as false , no matter if it is set to true or false , even if I call the function as

string folder = createDataFolder(setPBC, distribution, timestep, simtime, urange, ustrength, 
     particlesize, includeSteric, ranRod, ranU, rand, dvar, polydiam, true);

If I change the definition of the function definition and call, such that there is another parameter after Pointq , then Pointq is passed correctly, and the last parameter as well.

string createDataFolder(bool setPBC, string distribution, double timestep, double simtime, double potRange, double potStrength, 
    double particlesize, bool steric, bool ranRod, bool ranU, bool rand, double dvar, double polydiam, bool Pointq, bool tmp){
    //...
    if (Pointq) folder += "/pointq";
    //....
    return folder;
}

string folder = createDataFolder(setPBC, distribution, timestep, simtime, urange, ustrength, 
     particlesize, includeSteric, ranRod, ranU, rand, dvar, polydiam, Pointq, true)

If I change the order of the last two arguments of the createDataFolder function, it works as well.

string createDataFolder(bool setPBC, string distribution, double timestep, double simtime, double potRange, double potStrength, 
    double particlesize, bool steric, bool ranRod, bool ranU, bool rand, double dvar, bool Pointq, double polydiam){ ... }

I assume there is a stupid mistake in my code, but I don't know where to look, since I have no intuition, how such an error could occur. I've searched the forum for something similar in C++ but I was not able to find anything.

It would be great if somebody could provide me with some insights or point me to a relevant thread.


EDIT

Here's a minimal example that still produces the error on my machine

#include <iostream>


using namespace std;

void createDataFolder( double potRange, bool Pointq){
    char range[5];
    sprintf(range, "%.3f", potRange);
    cout << "in createDataFolder Pointq is " << Pointq << endl;
}

int main(int argc, const char* argv[]){    
    bool Pointq = true;
    double urange = 10;

    cout << "In main(), Pointq is " << Pointq << endl;
    createDataFolder( urange, Pointq);
    return 0;
}

in this code:

char range[5];
sprintf(range, "%.3f", potRange);

you pass 10.0 to potRange , so sprintf should produce string "10.000", which is definitely longer than 5. So you have buffer overrun and all effects with that UB. You should use snprintf in such cases to avoid long time spent on debugging side effects:

char range[5];
snprintf(range, sizeof(range), "%.3f", potRange);

it would not make your program correct, but at least problem would become obvious.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM