关于main参数 *argv[] 指向空间放在哪的疑问
来源:互联网 发布:廖雪峰python教程 编辑:程序博客网 时间:2024/05/11 19:25
一、问题来由
由以上疑问,演化成了“只在编译期确定的空间才会分配在栈上”的疑问,因为上面 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提出的问题
- 关于main参数 *argv[] 指向空间放在哪的疑问
- 关于main函数的参数argc,argv
- main(int argc, char **argv)参数argc,argv说明和指向指针的指针变量
- 关于main函数的argc和argv参数
- 关于main主函数的两个参数argc和argv
- main()的参数argc,argv
- 关于int main( int argc, char* argv[] ) 中arg和argv参数的解析及调试
- main函数的参数 argc argv env
- main函数的参数argc与argv
- MAIN函数的参数argc, argv说明
- 由 argv引出的main参数
- main函数的参数详解argc argv
- main函数里面的参数argv是什么?
- Python main的命令行参数:sys.argv
- main函数的参数:argc, argv[]
- main 函数的两个参数argc、argv
- main函数的参数:argc和argv
- main()函数的参数 int main(int argc, char* argv[])
- redis 安装 启动
- CSS3实现翻转(Flip)效果
- 本地音频播放,使用AVFoundation.framework中的AVAudioPlayer来实现
- Mother's Milk--DFS
- Java 回顾笔记_多线程_线程通讯1
- 关于main参数 *argv[] 指向空间放在哪的疑问
- 详细记录python的range()函数用法
- 将自定义的布局设置给Dialog
- 数据库报表
- ubuntu下安装putty
- STL学习之函数对象
- repo 签出某个Android版本
- ios-基础之【6】-NSString
- Runaround Numbers--模拟