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?
argv is an array of pointers to char (i.e. array of strings). The length of this array is stored in argc argument. strlen is meant to be used to retrieve the length of the single string that must be null-terminated else the behavior is undefined.
He said the memory size of argv in int main(int argc, char **argv) is 48 bytes, including itself.
argv(ARGument Vector) is array of character pointers listing all the arguments. If argc is greater than zero,the array elements from argv[0] to argv[argc-1] will contain pointers to strings. Argv[0] is the name of the program , After that till argv[argc-1] every element is command -line arguments.
The value of the argc argument is the number of command line arguments. The argv argument is a vector of C strings; its elements are the individual command line argument strings. The file name of the program being run is also included in the vector as the first element; the value of argc counts this element.
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 the sysconf()
function with the SC_ARG_MAX
parameter.
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 the main()
function without invoking undefined behaviour. Functions such as GNU getopt()
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 the argv
as provided to main()
.
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 as int main(int argc, char **argv, char **envp)
in some systems (recognized as an extension in the C standard Annex J, §J.5.1), where envp
is the same value as is stored in the global variable environ
, and is the start of a null-terminated array of pointers to the environment strings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With