简体   繁体   中英

Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) error while using scanf

I am trying to input data using scanf function for simulation. However, it is giving the above error at scanf. Some part of code is given below. It was working earlier but now it throwing this error **Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) **

#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <random>
#include <chrono>
#include "normdist.h"
using namespace std;
double initial_stock_price, expiration_time, volatility, R;
int no_of_discrete_barriers, no_of_trials;

int main(int argc, char* argv[])
{

    sscanf(argv[1], "%lf", &expiration_time);
    sscanf(argv[2], "%lf", &risk_free_rate);
    sscanf(argv[3], "%lf", &volatility);
    sscanf(argv[4], "%lf", &initial_stock_price);
    sscanf(argv[5], "%lf", &strike_price);
    sscanf(argv[6], "%d", &no_of_trials);
    sscanf(argv[7], "%d", &no_of_discrete_barriers);
    sscanf(argv[8], "%lf", &barrier_price);

Are you calling the program with arguments separated by a comma? You should be calling the program like this:

./program 1 0.05 0.25 50 20 100000 25 20

When I call it this way it does not crash on my computer.

Also, as commented above, always check in the beginning of the program if argc is >= 8, if not - display program's usage and exit.

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