使用“ new”的c ++动态存储器分配。 [英] c++ dynamic memory allocation using "new"

查看:72
本文介绍了使用“ new”的c ++动态存储器分配。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++的新手,尝试自己学习(我有Java背景)。

I'm new to C++, trying to learn by myself (I've got Java background).

我可以分配这种动态内存分配的概念到使用 new 的数组。

There's this concept of dynamic memory allocation that I can assign to an array (for example) using new.

在C(以及C ++)中,我已经 malloc realloc 正在执行此操作。由于某些我无法理解的原因,他们在C ++中添加了

In C (and also in C++) I've got malloc and realloc that are doing that. In C++ they've added the new for some reason I can't understand.

我读了很多关于普通数组进入堆栈而动态分配数组进入堆之间的区别。

I've read a lot about the difference between a normal array that goes to the stack while the dynamic allocated array goes to the heap.

所以我的理解是,通过使用 new 我在堆中分配了不会被删除的空间假设某个函数完成后会自动执行,但是直到我最终手动释放它之前,它都会保留在原处。

So what I understand is that by using new I'm allocating space in the heap which will not be deleted automatically when finished a function let's say, but will remain where it is until I finally, manually free it.

我找不到在普通内存上使用动态内存分配的实际示例。

I couldn't find practical examples of using the dynamic memory allocation over the normal memory.


  1. 据说使用普通数组时无法通过运行时分配内存。好吧,可能我不太了解它,因为当我尝试创建普通数组(没有 new )时,其容量由用户输入(例如 arr [input] ),效果很好。

  1. It's said that I can't allocate memory through runtime when using normal array. Well, probably I didn't understand it right because when I tried to create a normal array (without new) with a capacity given as an input by the user (like arr[input]), it worked fine.

这是我的意思:

int whatever;
cin>>whatever;

int arr2[whatever];

for (int i = 0; i < whatever; i++) {
    arr2[i]=whatever;
    cout<<arr2[i];

}




  1. 我不是真的了解扩展数组容量的唯一方法是将其复制到 new 较大数组时为什么将其称为动态。

  1. I didn't really understand why it's called dynamic when the only way of extending the capacity of an array is to copy it to a new, larger array.

我知道Vector类(我还没有学过)要好用得多。但是,我仍然不能离开知识鸿沟,我必须理解为什么准确地称它为动态,为什么我应该使用它而不是普通数组。
为什么当我不能真正扩展内存而只能将其复制到新数组时,应该手动手动释放内存?

I understood that the Vector class (which I haven't yet learned) is much better to use. But still, I can't just leave that gap of knowledge begin and I must understand why exactly it's called dynamic and why should I use it instead of a normal array. Why should I bother freeing memory manually when I can't really extend it but only copy it to a new array?

推荐答案

当您在编译时知道数组的大小时,可以这样声明它并将其保存在堆栈中:

When you know the size of an array at compile time you can declare it like this and it will live on the stack:

int arr[42];

但是,如果您不知道编译时的大小,仅在运行时,则不能说:

But if you don't know the size at compile time, only at runtime, then you cannot say:

int len = get_len();
int arr[len];

在这种情况下,您必须在运行时分配数组。在这种情况下,数组将驻留在堆上。

In this case you must allocate the array at runtime. In this case the array will live on the heap.

int len = get_len();
int* arr = new int[len];

当您不再需要该内存时,需要执行删除[] arr

When you no longer need that memory you need to do a delete [] arr.

std :: vector 是一个可变大小的容器,可让您

std::vector is a variable size container that allows you to allocate and reallocate memory at runtime without having to worry about explicitly allocating and freeing it.

int len = get_len();
std::vector<int> v(len); // v has len elements
v.resize(len + 10); // add 10 more elements to the vector

这篇关于使用“ new”的c ++动态存储器分配。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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