我在 Linux 上工作。命令行参数存储在内存的哪个部分(堆栈或堆)?
我尝试执行 free(argv)
,但遇到了段错误。这是为什么?
最佳答案
I tried to execute free(argv) and I got a segmentation fault. Why is that?
您只能free
您malloc
/calloc
(以后可能realloc
)。尝试 free
其他东西会调用未定义的行为。 UB 表现出来的一种(好的)方式是产生段错误;一种(不好的)方法是使程序看起来按预期工作。
至于它们在哪里……请阅读 C99 Standard 的第 5.1.2.2.1 节-- 它的未指定。
the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.
关于c - 操作系统在哪里存储命令行参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7728191/