C argv 数据的最大大小是多少 [英] C argv what is the maximum size of data

查看:83
本文介绍了C argv 数据的最大大小是多少的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
关于主函数的命令行参数

我如何确定可以传递给 C main(int argc, char* argv) 的最大数据大小?标准中是否有一个宏可以定义这个?数据是由主进程拥有"(即我的程序是否存储这些数据)还是以某种方式由操作系统拥有",我只能获得指向它的指针?

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?

推荐答案

在 POSIX 系统中,有一个值,ARG_MAX,定义在 ,最小可接受值_POSIX_ARG_MAX(即 4096).您可以在运行时通过 sysconf()SC_ARG_MAX 参数的 code> 函数.

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.

通常为 256 KiB.

It is often 256 KiB.

argv 中的数据(指针数组和它们指向的字符串)由程序拥有".它们可以被修改;这是否合理取决于你的观点.您当然不能在不调用未定义行为的情况下超出传递给 main() 函数的范围.GNU getopt() 等函数在没有在环境中设置 POSIXLY_CORRECT 环境变量的情况下运行时会重新组织参数.您已经有一个指向 argv 中数据的指针,如提供给 main() 的那样.

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().

根据经验,你经常会发现紧跟在字符串argv[argc-1]结尾之后的数据实际上是环境的开始.主程序在某些系统中可以写成int main(int argc, char **argv, char **envp)(在C标准附件J,§J.5.1中被认为是扩展),其中 envp 与存储在全局变量 environ 中的值相同,并且是指向环境字符串的以空字符结尾的指针数组的开头.

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.

这篇关于C argv 数据的最大大小是多少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆