关于 main 函数的命令行参数

2022-01-21 00:00:00 main c arguments c++

它看起来像 int main(int argc, char *argv[]);.我的问题是:

It will look like int main(int argc, char *argv[]);. My questions are:

1 我可以在 argv[] 中添加多少个数组项?

1 How many array items can I add in argv[]?

2 每个 char * 的最大大小是多少?

2 What is MAX size of every char *?

推荐答案

你可以试试:

$ getconf ARG_MAX
2180000

http://pubs.opengroup.org/onlinepubs/007904975/basedefs/limits.h.html

ARG_MAX 是 exec 函数的最大参数长度,包括环境数据.

ARG_MAX is maximum length of argument to the exec functions including environment data.

也就是说,参数的数量或参数的长度没有单独的限制.只有存储所有参数和环境变量所需的总大小限制.

That is, there is no individual limit on the number of arguments or argument's length. Only the limit on total size required to store all the arguments and environment variables.

xargs 使用 计算最大命令行长度sysconf(_SC_ARG_MAX); 生成的值与 getconf ARG_MAX 报告的值相同.

xargs figures out maximum command line length using sysconf(_SC_ARG_MAX); which yields the same value as reported by getconf ARG_MAX.

在 Linux 上,命令行参数和环境变量被放入新进程的堆栈中.因此,进程/线程最大堆栈大小是最终上限.Linux 特定的限制是硬编码在内核中:

On Linux command line arguments and environment variables are put into new process' stack. So, the process/thread maximum stack size is the ultimate upper bound. Linux-specific limits are hardcoded in the kernel:

#define MAX_ARG_STRLEN (PAGE_SIZE * 32)
#define MAX_ARG_STRINGS 0x7FFFFFFF

相关文章