Possible Duplicate:
About command line arguments of main function
How would I determine what the maximum size of data I could pass into a C main(int argc, char* argv)? Is there a macro somewhere in the standard that would define this? Is the data “owned” by the main process (i.e. does my program store this data) or is it somehow “owned” by the operating system and I can just get a pointer to it?
In a POSIX system, there is a value,
ARG_MAX, defined in<limits.h>with a minimum acceptable value of_POSIX_ARG_MAX(which is 4096). You can discover the value at run-time via thesysconf()function with theSC_ARG_MAXparameter.It is often 256 KiB.
The data in
argv(both the array of pointers and the strings that they point at) are ‘owned’ by the program. They can be modified; whether that is sensible depends on your viewpoint. You certainly can’t step outside the bounds of what was passed to themain()function without invoking undefined behaviour. Functions such as GNUgetopt()do reorganize the arguments when run without the POSIXLY_CORRECT environment variable set in the environment. You already have a pointer to the data in theargvas provided tomain().Empirically, you will often find that the data immediately after the end of the string
argv[argc-1]is actually the start of the environment. The main program can be written asint main(int argc, char **argv, char **envp)in some systems (recognized as an extension in the C standard Annex J, §J.5.1), whereenvpis the same value as is stored in the global variableenviron, and is the start of a null-terminated array of pointers to the environment strings.