Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I assign a value to argv?

When I use vscode for c and c++ debugging, sometimes some command line parameters are needed(./runFile args1 args2). If I directly click to run, it will display argv[2] out of range. This is because I did not enter parameters. But if I enter the parameters through the command line, it seems that I cannot step through vscode. The method I thought of is that I use argv[]=value to assign the command line parameters here at the beginning of the main function. Will there be a problem with this use? And I can only argv[0]=value argv[1]=value. Is there a way to directly assign a value to argv through argv=...? I tried argv[1]=value argv[2]=value. Can operate normally. Are there any risks here? For example, how much memory is allocated for argv?

like image 908
Gerrie Avatar asked Dec 22 '22 16:12

Gerrie


2 Answers

When vscode runs your program, it uses parameters found in a file named launch.json to do it.

If you got to: Run -> Open Configurations, that'll open the launch.json for that project. By default it'll look something like this:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "enter program name, for example ${workspaceFolder}/a.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

If you hover over the args, it'll tell you: "Command line arguments passed to the program."

Rather than trying to modify argv after the program starts, I'd try putting the arguments you want in there, and see if it won't pass those arguments to your program like it says it will.

like image 169
Jerry Coffin Avatar answered Jan 13 '23 13:01

Jerry Coffin


Can I assign a value to argv?

Yes.

Another matter is whether you should. I don't recommend it.

For example, how much memory is allocated for argv?

There is guaranteed to be argc + 1 number of pointers in the array pointed by argv. The last being a null pointer. There is no guarantee that more memory would be allocated.


P.S. I haven't used vscode, but I suspect that it has a way of specifying command line arguments when running a debugger inside it. Most IDE's do anyway. If not, then perhaps you can use a command line debugger.

like image 43
eerorika Avatar answered Jan 13 '23 12:01

eerorika