关于main参数 *argv[] 指向空间放在哪的疑问

简介:

一、问题来由

由以上疑问,演化成了“只在编译期确定的空间才会分配在栈上”的疑问,因为上面 argv 指向明显是编译期确定不了的,那会放在堆上吗?

二、讨论

在书上找到如下段落:

这里写图片描述

(1)栈内存用来保存定义在函数内的非static对象;

(2)静态内存用来保存局部static对象、类static数据成员以及定义在任何函数体职位的变量。

通过以上段落,可以得到如下结论:

main参数 *argv[] 指向不可能存在栈上

因为这块空间不是在栈内定义的,但是argv这个指针在main函数栈上。

三、真相

argv的申请和释放应该由系统来负责,是传入参数,算不上全局或者局部变量。所以这块内存就不在程序内部。

详见网站 http://publications.gbdirect.co.uk/c_book/chapter10/arguments_to_main.html

There are at least two arguments to main: argc and argv. The first of these is a count of the arguments supplied to the program and the second is an array of pointers to the strings which are those arguments—its type is (almost) ‘array of pointer to char’. These arguments are passed to the program by the host system’s command line interpreter or job control language.

The declaration of the argv argument is often a novice programmer’s first encounter with pointers to arrays of pointers and can prove intimidating. However, it is really quite simple to understand. Since argv is used to refer to an array of strings


参考资料

[1] http://bbs.csdn.net/topics/390759615

[2] http://publications.gbdirect.co.uk/c_book/chapter10/arguments_to_main.html

[3] http://en.cppreference.com/w/cpp/language/main_function

[4] 我在quora提出的问题

相关文章
|
1月前
|
程序员 C语言 开发者
int main() 作用详解
int main() 作用详解
53 0
|
23天前
|
NoSQL C++
c++中包含string成员的结构体拷贝导致的double free问题
c++中包含string成员的结构体拷贝导致的double free问题
8 0
|
1月前
|
存储 Shell C语言
指针数组组main函数的形参
指针数组组main函数的形参
10 0
|
4月前
|
存储
char**传参,获取函数内申请的字符串,外部释放
char**传参,获取函数内申请的字符串,外部释放
31 0
|
8月前
|
C#
C#基础之Main函数
C#基础之Main函数
53 0
|
9月前
|
C++ Windows
VS2019错误:_main已经在.obj中定义,找到一个或多个重定义符号
VS2019错误:_main已经在.obj中定义,找到一个或多个重定义符号
|
10月前
编写一个程序,初始化一个double类型的数组,然后把该数组的内容拷贝至3个其他数组中(在main()中声明这4个数组)。
编写一个程序,初始化一个double类型的数组,然后把该数组的内容拷贝至3个其他数组中(在main()中声明这4个数组)。
|
10月前
编写一个程序,链接两个字符串字面常量,将结果保存在一个动态分配的char数组中,重写这个程序,连接两个标准string对象
编写一个程序,链接两个字符串字面常量,将结果保存在一个动态分配的char数组中,重写这个程序,连接两个标准string对象
|
10月前
模拟实现字符串函数以及内存函数(均包含在头文件“string.h“中)
①不限字符个数型(strlen, strcpy, strcmp, strcat,strstr) ②限字符个数型(strncpy, strcmp, strncat) ③内存型(memcpy, memmove, memset)
57 0
用int main检验任意函数,如(func)函数
用int main检验任意函数,如(func)函数
72 0
用int main检验任意函数,如(func)函数