0

I'm trying to run a simple calculator program by passing it the numbers and the operation from the command line, such as "25.2 + 4.87". It works fine when using the default values but once I pass in the arguments, it throws the following message: "Exception thrown: read access violation. argv was 0xFEFEFEFE"

Here's the code:

//Valores por default
    char num1[] = "0.5";
    char num2[] = "10.25";
    char operacion = '/';
    if (argv[1] && argc ==4) { 
        std::cout << argv[1] << std::endl;
        std::cout << argv[2] << std::endl;
        std::cout << argv[3] << std::endl;
        strcpy_s(num1, 100, argv[1]);
        strcpy_s(num2, 100, argv[3]);
        operacion=*argv[2];
        
    }

It prints out the arguments fine but after running the program it throws the error I mentioned earlier after executing the second strcpy_s:

strcpy_s(num2, 100, argv[3]);

The first strcpy_s works fine and num1 gets assigned the first number I passed in the arguments, but anything else I do after that throws the error.

While debugging line by line I noticed that the variable "argv" has the correct path for my executable file, but after executing the first strcpy_s, the content changes from the correct file path to 0xfefefefe

I've looked for this error but all of them had a nullptr error, I couldn't find any information on this. I would greatly appreciate your help, thanks.

PMieres
  • 1
  • 1
  • 4
    `char num1[] = "0.5";` This makes `num1` an array of `4` characters. `strcpy_s(num1, 100, argv[1]);` This copies up to `100` characters to `num1`, which overruns the buffer and corrupts the stack.. – dxiv Sep 02 '20 at 04:40
  • I suugest to use `std::stod(std::string{argv[1]})` directly – asmmo Sep 02 '20 at 04:43
  • If you are going to `strcpy_s(num1, 100, argv[1]);` then you need to `char num1[100] = "0.5";` – Jerry Jeremiah Sep 02 '20 at 04:45
  • 2
    Replace `char num1[] = "0.5";` with `char num1[100];` or go full C++ and use a `std::string` Then you don't have to worry about the size. – user4581301 Sep 02 '20 at 04:45
  • Also the reason I'm not changing it to double or float is because I have to work with char arrays initially as part of my assignment. – PMieres Sep 02 '20 at 04:45
  • 1
    You are not allocating enough memory to `strcpy_s` into. `num1` can hold 4 bytes max, but `argv[1]` points to a string that is 5 bytes in size, so you overflow into surrounding memory. And worse, you are telling `strcpy_s` that both `num1` and `num2` are 100 bytes in size, which is clearly not true. Use `char num1[100] = ...` instead, same with `num2`. Also, `if (argv[1] && argc ==4)` should be `if (argc == 4 && argv[1])`, or simply `if (argc == 4)` as you don't need to check `argv[1]` for null if `argc` is > 1 – Remy Lebeau Sep 02 '20 at 04:49
  • It works after I used char num1[100] and num2[100], thanks. – PMieres Sep 02 '20 at 04:59
  • Please, keep in mind that this will work for less than 100 characters in arguments only. With `std::string` or not copying `argv`s (like already recommended), you would be on the safe side. (Please, don't ask me why a user of your app should input more than 100 characters. According to my experience, users are good for any evil sabotage...) – Scheff's Cat Sep 02 '20 at 05:50
  • 0xfefefefe is the Microsoft's standard library's way to mark freed memory on the heap in debug mode. https://stackoverflow.com/a/127404/3150802 – Peter - Reinstate Monica Sep 02 '20 at 07:13

0 Answers0