Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segmentation fault when parsing double #250

Closed
pfeatherstone opened this issue Dec 16, 2022 · 3 comments
Closed

Segmentation fault when parsing double #250

pfeatherstone opened this issue Dec 16, 2022 · 3 comments

Comments

@pfeatherstone
Copy link

 argparse::ArgumentParser args("Demo");
 args.add_argument("--gain").help("Gain").required().default_value(40.0);

try {
        args.parse_args(argc, argv);
    }
    catch (const std::runtime_error& err) {
        std::cerr << err.what() << std::endl;
        std::cerr << args;
        std::exit(1);
    }

auto val = args.get<double>("--gain");
    printf("%f\n", val);

By default, it runs fine and prints 40.000.
If i run with --gain 30.0, I get a segmentation fault

@LAhmos
Copy link

LAhmos commented Dec 26, 2022

same with int

@skrobinson
Copy link
Contributor

Fortunately, this is a simple fix for double, int, and all numeric types. The segfault is because --gain is a std::string wrapped in a std::any and .get<double>("--gain") asks for the string to be cast to a double.

The fix is to use .scan<'g', double>() when adding the Argument. Or use .scan<'i', int>() for an int.

See Converting to Numeric Types for details.

@lingerer
Copy link

lingerer commented Jan 9, 2023

#225 is related

@p-ranav p-ranav closed this as completed Oct 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants