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.
man 3 exec
and the mana fromSEE ALSO
in it. Perhaps, besides them, theman system
andman 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 pmexecl("a.exe", "xaxa", 0);
also works. - avp