Hello!

Recently I started learning C ++, and I don’t understand why I’m taking 2 arguments? One is an integer, and the second is an array, and memory is allocated somehow strangely without new . Where do these arguments come from, and why are they used?

  • 3
    I can only advise you on these answers for a deeper understanding of reading man 3 exec and the mana from SEE ALSO in it. Perhaps, besides them, the man system and man popen are of interest. - By the way, in general, the main argument is not 2, but 3 arguments (the 3rd array of addresses of environment variables (environment), terminated by zero. It is similar to the second one, in which the addresses of the command line arguments are passed) int main (int ac, char ** av, char ** env); - avp pm
  • Far from the fact that a person has Linux. and the third parameter is compiler-dependent, although many support it. In principle, no one prevented developers from doing something like int main (std :: vector <std :: string> argv) {return 0; } But they are not in a hurry. - KoVadim
  • @KoVadim, I don’t know which compilers are like, and g ++ in Windows remarkably supports this: c: / Users / avp / src / cc / hashcode $ g ++ 3args.cpp c: / Users / avp / src / cc / hashcode $. / a a1 a2 arg av [0]: c: \ Users \ avp \ src \ cc \ hashcode \ a.exe av [1]: a1 av [2]: a2 env env [0]: ALLUSERSPROFILE = C: \ ProgramData env [1]: APPDATA = C: \ Users \ avp \ AppData \ Roaming .... env [53]: windows_tracing_flags = 3 env [54]: windows_tracing_logfile = C: \ BVTBin \ Tests \ installpackage \ csilogfile.log c: / Users / avp / src / cc / hashcode $ - Yes, and execl("a.exe", "xaxa", 0); also works. - avp
  • yes, @KoVadim with Linux obviously got excited)) - mega

3 answers 3

These are command line parameters that can be passed to your application. The first argument is passed for the reason that the built-in arrays in C / C ++ do not store size and size transfer — one way to find out about this (for example, you can agree that the last element will be zero, but this imposes certain restrictions).

memory is allocated somehow strange without new

memory is allocated elsewhere, and only the pointer comes into the function. How exactly it stands out there - at the moment you should not be disturbed. The main thing to remember is the rule - "who allocates memory, he usually removes it". In this case, the compiler will insert all the necessary code that will allocate memory and free it. Although there is also a little system can select.

How come the arguments?

For example, you can run the program so

 myprog param1 param2 param3 

in this case, argc will have the value 4. argv [1] will be equal to param1. argv [3] will be equal to param3. The question arises, what is in the zero element? and there is the name of the program in the form in which it is provided by the operating system. That is, there can be either myprog or the full path name.

Additional material:

upd: for lovers of the standard - read paragraph 3.6.1. It says in particular that there are officially two options - int main() and int main(int argc, char *argv[]) (although the latter can be written as int main(int argc, char **argv) - the essence does not change) . Everything else is at the discretion of the compilers.

  • one
    I add that in posix ОС , ultimately, the program is started (replacing the executable code and data of the current process) by calling int execve (const char * filename, char * const argv [], char * const envp []); Moreover, the programmer is responsible for the contents of argv[0] !!! (and not the system). It should be added that regardless of the form the main() environment (the third parameter execve() ) in posix always accessible through extern char ** environ; Of course, Windows is not posix , but in gcc / g ++ the exec() family and environ implemented. Interestingly, in other Windows C / C ++ compilers, is this so? - avp

When you start the program, you can specify additional. command line arguments

The first parameter, argc (argument count), indicates the number of these arguments, and the second parameter, argv (argument vector), contains the arguments themselves that were passed at startup.

    These are command line arguments. argc is the number, argv is the argument array itself. You can display them on the screen and see what happens. Zero is always the name of the program, then - anything.

    Used, for example, as follows: You write something to work with files. When you start the program, you naturally ask the user to specify the path to the file with which to work. But it’s much more convenient to start the program from the terminal with the already specified parameter: Admin:~ prog ~/Documents/file1.txt - in this case, the terminal itself can add the name of the file or something else convenient to do. And in Windows, you can also drag and drop a file onto the program (here I don’t remember exactly how the file name is transferred).

    Again, this is only an example - only your imagination limits you.

    UPD: while writing already answered))

    • Note that the null argument (just like the rest) is controlled by the caller, and its value is not guaranteed. - VladD
    • This is true, only in gcc / g ++ MinGW (Windows) there is an interesting nuance. Here in this code char * const argv [] = {0}; execve (av [0], argv, environ); the system (?) still forms argv [] of 2 elements, in argv [0] it puts the file name with the extension. But in Linux everything is fair - argv [0] == 0. - avp