什么是C ++中的动态内存分配? [英] What is Dynamic Memory Allocation in C++?

查看:275
本文介绍了什么是C ++中的动态内存分配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习C ++中的动态内存分配,并提到关键字 new new []
据说允许用户在运行时指定内存分配的大小,不同于在源代码中声明一个固定大小的变量或数组。

I'm learning about Dynamic Memory Allocation in C++ and the keywords new and new[] are mentioned. It is said to enable users to specify the size of the memory allocation at runtime, unlike simply declaring a variable or array with a fixed size in the source code.

我不明白这个概念。它是如何工作的?

I don't understand this concept. How does it work? I just need a clarification on the idea and an example would be helpful!

推荐答案

因此,如果你想要一个10个整数的数组,你会写:

So, if you want an array of 10 integers, you'd be writing:

int arr[10]; 

但如果你想做这样的事情会怎么样?

But what if you wanted to do something like this;

cout << "How many?";
cin >> num;

int arr[num];

好吧,C ++语言不允许。您必须执行以下操作:

Well, the C++ language doesn't allow that. Instead, you have to do:

int *arr = new int[num]; 

来创建数组。后来你必须[1]使用:

to create your array. And later on you MUST[1] use:

delete [] arr; 

可释放内存。

那么,这是如何工作的?当你调用new时,C ++运行时库[你不必编写的代码构成了C ++的基本原理]将会得出 num 整数有多少空间并在内存中找到一些空间。我不打算详细说明你如何找到一些记忆。

So, how does this work? When you call new, the C++ runtime library [the code that you didn't have to write that makes up the fundamentals of C++] will figure out how much space num integers take up, and find some space in memory for that. I'm not going into details of "how you find some memory". For now, just trust me, there is some memory available somewhere that can be used to store some integers in.

当你稍后调用 delete

When you later call delete, that same memory is given back to the "pool" or "heap" of memory that it came from.

当然,如果你有一个机器,例如256 MB的内存,你试图要求空间存储2.5亿整数,整数占用多个字节,它不会工作 - 这里没有神奇 - 内存仍然限制在机器中有多少可用...你只是有权利在程序中确定,当它运行时,需要多少内存,而不必决定何时编写程序。

Of course, if you have a machine with, say, 256 MB of memory, and you try to ask for space to store 250 million integers, bearing in mind that an integer takes up more than one byte, it's not going to work out - there is no "magic" here - the memory is still limited to how much is available in the machine.... You just have the right to determine in the program, when it's running, how much memory you need, rather than having to decide when WRITING the program.

编辑:通常最好是隐藏任何内存分配已经存在的对于这个目的有用的container-和wrapper-classes。例如:

It is generally best to "hide" any memory allocation using the already existing "container-" and "wrapper-classes" that are useful for this very purpose. For example:

 std::vector<int> arr;

将作为整数的变量存储,您不必担心释放内存,甚至知道在你存储它们之前需要多少。

would work as a variable storage for integers, and you never have to worry about freeing the memory, or even knowing how many you need before you have stored them there.

 std::shared_ptr<int> arr = new int[num]; 

是另一种情况,当shared_ptr不再使用时共享指针类,所以你永远不需要关心释放内存]。

is another case, where when the "shared_ptr" is no longer in use [it track that inside the shared pointer class, so you never need to care about freeing the memory].

[1]如果你不想泄漏内存,它是坏风格泄漏内存。不要让任何人快乐,如果你这样做。

[1] If you don't want to leak memory, and it's "bad style" to leak memory. Not making anyone happy if you do.

这篇关于什么是C ++中的动态内存分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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