内存分配和** argv参数 [英] Memory allocation and **argv argument

查看:125
本文介绍了内存分配和** argv参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我们使用这个参数,我甚至知道如何使用这个参数。

I know for what we use this argument, and I even know how to work with the argument.

只有一件事我还是不明白。程序如何为来自输入的字符串分配内存。 ** argv在程序的开始没有分配内存,不是吗?我期待着segfault,但没有发生。

There is only one things I still do not understand. How a program allocate memory for strings which came from the input. **argv has no allocated memory at the start of the program, isn't it? I was expecting segfault, but it didn't happen.

有没有人知道这个内存分配是如何工作的?

Does anybody know how this memory allocation work?

推荐答案

C / C ++运行时处理命令行参数,并创建一个内存区域放置参数。

The C/C++ runtime processes the command line arguments and creates an area of memory where the arguments are put. It then calls your main() providing you a count of the number of arguments along with a pointer to the area where the arguments are stored.

因此,C / C ++运行时环境变量拥有所分配的内存区域,一旦您的main()返回或如果使用其他C / C ++函数来停止程序,如exit(),则由C / C ++运行时释放该区域。

So the C/C++ runtime owns the memory area allocated and it is up to the C/C++ runtime to deallocate the area once your main() returns or if some other C/C++ function is used to stop the program such as exit().

此过程起源于在Unix下使用C,并作为提供C ++委员会试图维护的向后兼容性程度的一部分保存为C ++。

This procedure originated with the use of C under Unix and was kept for C++ as a part of providing the degree of backwards compatibility the C++ committee has tried to maintain.

通常,当程序加载时,加载器启动的入口点不是main()函数,而是C / C ++运行时中定义的入口点。 C / C ++运行时执行各种初始化来设置C / C ++标准说明在C / C ++运行时一旦初始化完成时调用main()函数时就存在的环境。

Normally when your program loads, the entry point that is started by the loader is not your main() function but rather an entry point defined in the C/C++ runtime. The C/C++ runtime does various kinds of initialization to setup the environment that the C/C++ standards say will exist at the point when the main() function is called by the C/C++ runtime once the initialization is completed.

初始化期间的一个步骤是提供的命令行参数的解析,然后将其提供给main()函数作为其函数参数。

One of the steps during this initialization is the parsing of command line arguments provided which are then provided to the main() function as its function arguments.

这篇关于内存分配和** argv参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆