C++:“新”是什么意思?一个会不断增长的收藏?
我是 C++ 新手。 “新”收藏到底意味着什么?例如:
UnicodeStringList* tmp = new UnicodeStringList;
// where UnicodeStringList is typedef to std::list<UnicodeString>
当你“新”某样东西时,你必须确切地知道你需要它有多大,对吗?所以当我 使用赋值构造函数复制一个对象,计算机如何知道应该在堆上分配多少内存?例如:
*tmp = another_string_list;
another_string_list 被复制到堆内存中的新 UnicodeStringList 中,但我最初从未指定堆内存应该有多大。并且编译器不知道 another_string_list 有多大,那么有多少内存进入堆?
我很困惑,希望我已经足够详细地说明了我的问题,以便有人可以理解我,但我不确定。
请帮忙
谢谢,
朱利安
I am new to C++. What does it mean exactly to "new" a collection? For example:
UnicodeStringList* tmp = new UnicodeStringList;
// where UnicodeStringList is typedef to std::list<UnicodeString>
When you "new" something you have to know exactly how big you need it to be, right? So when I
use the assignment constructor to copy an object, how will the computer know how much memory should be allocated on the heap? For example:
*tmp = another_string_list;
another_string_list is being copied into my new'd UnicodeStringList in heap memory, but I never initially specified how big that heap memory ought to be. And the compiler doesn't know how big another_string_list is so how much memory goes into the heap?
I am confused and hopefully I've specified my question enough so someone may understand me, but I'm not sure.
Please help
Thanks,
Julian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当您向其中添加元素时,
std::list
的大小不会改变。我将使用std::vector
因为该示例更简单,但同样的概念适用:std::vector
包含一个指向数组的指针,该数组根据需要动态调整大小以包含您的元素。即使它指向的数组发生变化,指向数组的指针的大小也不会改变(它是一个指针的大小)The size of a
std::list
doesn't change when you add elements to it. I'm going to use astd::vector
because the example is simpler, but the same concept applies: astd::vector
contains a pointer to an array, which is dynamically resized as needed to contain your elements. The pointer to the array doesn't change in size (it's the size of one pointer), even though the array it points to changes“new”所做的就是分配足够的空间来存储
std::list
的所有成员变量。任何可能需要做的额外事情都是 std::list 的事,它应该自己处理这些事情(通过它的构造函数和析构函数)。All that "new" does is allocate enough space to store all the member variables for your
std::list
. Anything extra that may need to be done is thestd::list
's business, and it should take care of that itself (via its constructors and destructors).不完全是。至少,不是你现在想的那样。
当您
新建
原始数组时,您当然必须提供数组中的元素数量。但std::list
、std::vector
等不是原始数组。以
std::list
为例:从外部来看,你可以将它视为包含你放入其中的任何内容的东西。然而,详细来说,它是一个直接只包含指针的对象。这些指针指向它在堆上分配的其他对象(使用new
)。因此,std::list 本身的实例始终具有相同的大小,但是当您向其中添加更多内容时,它最终会在堆上的其他位置分配更多内容来管理它。这也是为什么您可以使用列表作为堆栈分配的局部变量,并且可以轻松地将任意数量的项推入其中。
不需要
新的
。该列表安排其自己的内部(堆分配)簿记以适应我的项目和您想要添加到其中的项目。因此,当一个列表 A 分配给列表 B 时,列表 A 中的所有项目(以及任何内部管理的簿记对象)都将复制到新的堆分配项目中,并交给列表 B 进行管理。
Not exactly. At least, not in the way that you're thinking about it.
When you
new
a raw array then of course you have to provide the number of elements in the array. Butstd::list
,std::vector
, and such are not raw arrays.Taking
std::list
as an example: From the outside, you can think of it as something that contains whatever you put into it. However, in detail, it is an object that directly contains only pointers. Those pointers point to other objects that it has allocated on the heap (usingnew
). Thus an instance ofstd::list
itself is always the same size, however as you add more things to it, it will end up allocating more stuff elsewhere on the heap to manage it.This is also why you can use a list as a stack-allocated local variable and not have trouble pushing any number of items into it.
No need for
new
. The list arranges its own internal (heap allocated) bookkeeping to accommodate as my items and you want to add to it.And so when one list A assigned to list B. All items (and any internally managed bookkeeping objects) from list A are copied into newly heap allocated items and given to list B to manage.
您真正需要知道某个东西有多大的唯一地方是在堆栈上。堆栈必须以非常静态的方式增长才能满足编译器的要求。然而,堆没有这样的限制。
当您
新建
某些内容时,您会在堆上分配它。因此,它可以是任意大小。让您感到惊讶的是,集合也可以在堆栈上分配。这是因为无论集合包含什么,它的大小都是不变的。相反,它包含指向堆的信息(如指针),其中分配的大小必须是可变的。The only place where you actually have to know how big something will be is on the stack. The stack must grow in a very statical way to please your compiler. The heap, however, has no such constraints.
When you
new
something, you allocate it on the heap. Therefore, it can be of any size. What surprises you should rather be that collections can also be allocated on the stack. This is because no matter what a collection contains, its size is invariant. Rather, it contains informations (like pointers) to the heap where the size of an allocation must be variant.当您
new
和对象时,您将获得该对象在其初始状态下所需的内存。但是,您需要认识到,类在处理其内部状态的方式上可能会实现相当复杂的情况。具体来说,对于您的问题,类(在您的情况下是
list<>
)本身可以在其操作过程中动态分配和释放内存。这就是您的list
集合将要做的 - 当一个项目添加到列表中时,它将为该项目分配内存并执行管理该新项目所需的任何内务处理,通常使用指针或智能指针对象。因此,list<>
对象使用的内存可能会发生变化,但“核心”列表对象本身的大小仍与最初分配时的大小相同。When you
new
and object, you get the memory that is required for that object in it's initial state. However, you need to realize that a class can implement quite a bit of complexity in how it deals with its internal state.Specifically for your question, a class (in your case a
list<>
) can itself dynamically allocate and release memory during the course of it operations. And that's what yourlist
collection will do - when an item is added to the list, it'll allocate memory for that item and perform whatever housekeeping is necessary to manage that new item, usually using pointers or smart pointer objects. So the memory used by thelist<>
object may change, but the 'core' list object itself remains the same size as when it was allocated originally.您可能想了解一下 new 的作用。它将它放在堆内存而不是堆栈内存上。这样,当你失去范围时,它就不会消失。它与已知尺寸无关。
您可能会将其与数组混淆,当您为数组分配内存时,您需要知道大小,并且通常会新建它。
You might want to look up what new does. It puts it on the heap memory instead of stack memory. This way, it's not going to go away when you lose scope. It has nothing to do with the known size.
You might be confusing this with arrays, where when you allocate the memory for an array you need to know the size, and you typically new it.