内存分配和** argv的说法 [英] Memory allocation and **argv argument

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

问题描述

我知道我们使用的这种说法,我甚至知道如何与论证工作。

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

有只有一个东西,我还是不明白。如何一个程序这些来自输入字符串分配内存。 ** argv的曾在节目的开始没有分配的内存,是不是?我期待段错误,但它并没有发生。

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 ++运行时处理命令行参数,并创建其中参数被放在的内存区域。然后,它调用你的main()为您提供的参数个数的计数的指针,其中的参数存储区域沿。

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 ++运行时拥有的内存区域分配,这要由C / C ++运行时释放的区域,一旦你的main()返回,或者一些其他的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 ++标准说,将在该点存在时,在main()函数的调用环境中的C / C ++运行时一旦初始化完成。

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.

之一这个初始化过程中的步骤是提供命令行参数然后将其提供给主()函数作为其函数的参数解析

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天全站免登陆