在哪个内存段中存储命令行参数? [复制]
Posted
技术标签:
【中文标题】在哪个内存段中存储命令行参数? [复制]【英文标题】:In which memory segment command line arguments get stored? [duplicate] 【发布时间】:2013-09-11 22:18:26 【问题描述】:命令行参数argc
、arv[]
,与C语言中的main
函数一起使用,它们存储在内存的什么位置?
这个问题我要找的存储区域应该是堆、数据段、栈或者其他。
【问题讨论】:
我认为这是类似的问题,这里有答案***.com/questions/4196201/…: 【参考方案1】:它不是由语言指定的。
类 Unix 系统将它们放在堆栈上(或者在一些更深奥的系统中,“一个”堆栈,因为它们不止一个)。
【讨论】:
【参考方案2】:正如torek 在他的answer 中所说,C 语言没有定义必须存储命令行参数的位置,知道并没有太多实际用途。但是,信息通常存储在堆栈中。
此代码也使用main()
的通用扩展来获取环境,然后打印参数和环境的地址。
#include <stdio.h>
#include <inttypes.h>
int main(int argc, char **argv, char **envp)
printf("&argc = 0x%.8" PRIXPTR "\n", (uintptr_t)&argc);
printf("&argv = 0x%.8" PRIXPTR "\n", (uintptr_t)&argv);
printf("&envp = 0x%.8" PRIXPTR "\n", (uintptr_t)&envp);
while (*argv != 0)
printf("*argv = 0x%.8" PRIXPTR "\n", (uintptr_t)*argv++);
while (*envp != 0)
printf("*envp = 0x%.8" PRIXPTR "\n", (uintptr_t)*envp++);
return 0;
在我的 Mac 上运行时,它显示:
&argc = 0x7FFF5494B52C
&argv = 0x7FFF5494B520
&envp = 0x7FFF5494B518
*argv = 0x7FFF5494B748
*envp = 0x7FFF5494B74E
*envp = 0x7FFF5494B839
*envp = 0x7FFF5494B862
*envp = 0x7FFF5494B86D
*envp = 0x7FFF5494B889
*envp = 0x7FFF5494B8C1
*envp = 0x7FFF5494B8F4
*envp = 0x7FFF5494B905
*envp = 0x7FFF5494B915
*envp = 0x7FFF5494B920
*envp = 0x7FFF5494B92E
*envp = 0x7FFF5494B93B
*envp = 0x7FFF5494B974
*envp = 0x7FFF5494B998
*envp = 0x7FFF5494B9CD
*envp = 0x7FFF5494B9F2
*envp = 0x7FFF5494BA0B
*envp = 0x7FFF5494BA2A
*envp = 0x7FFF5494BA46
*envp = 0x7FFF5494BA7B
*envp = 0x7FFF5494BB2E
*envp = 0x7FFF5494BB3C
*envp = 0x7FFF5494BB76
*envp = 0x7FFF5494BB87
*envp = 0x7FFF5494BB9D
*envp = 0x7FFF5494BBC8
*envp = 0x7FFF5494BBEA
*envp = 0x7FFF5494BC2B
*envp = 0x7FFF5494BC35
*envp = 0x7FFF5494BC51
*envp = 0x7FFF5494BC5F
*envp = 0x7FFF5494BC75
*envp = 0x7FFF5494BC82
*envp = 0x7FFF5494BD55
*envp = 0x7FFF5494BD7E
*envp = 0x7FFF5494BD96
*envp = 0x7FFF5494BDA3
*envp = 0x7FFF5494BDAE
*envp = 0x7FFF5494BDBF
*envp = 0x7FFF5494BDCA
*envp = 0x7FFF5494BDE8
*envp = 0x7FFF5494BE01
*envp = 0x7FFF5494BE09
*envp = 0x7FFF5494BE1E
*envp = 0x7FFF5494BE38
*envp = 0x7FFF5494BE52
*envp = 0x7FFF5494BE71
*envp = 0x7FFF5494BE7D
*envp = 0x7FFF5494BEF5
*envp = 0x7FFF5494BF16
*envp = 0x7FFF5494BF27
*envp = 0x7FFF5494BF32
*envp = 0x7FFF5494BF66
*envp = 0x7FFF5494BF8D
这很乏味,但表明所有地址都在同一个通用区域,即 C 堆栈。 (在 Mac OS X 10.8.4 — 64 位程序上测试,在没有任何命令行参数和大型环境的情况下调用。)
【讨论】:
以上是关于在哪个内存段中存储命令行参数? [复制]的主要内容,如果未能解决你的问题,请参考以下文章