C ++使用指针指向指针向量有什么好处? [英] C++ Any benifits using pointer to vector of pointers?

查看:345
本文介绍了C ++使用指针指向指针向量有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开始之前,我已经看过以下问题:

Before I start, I have already looked at these questions:

指向指针向量的指针的内存消耗

指向矢量的指针vs指针向量vs指针向量指针

他们俩都没有回答我的问题.

And they both do not answer my question.

我正在从事一个涉及数百万个实例的项目.

I am working on a project where it involves millions of instances to work with.

我正在使用的数据结构第一次看起来有点复杂,该结构本身对于这个问题并不重要.

The data structure I am using is a bit complicated to look at for the first time, the structure itself isn't really of any importance for this question.

现在,如果我有以下内容:

Now, if I had the following:

vector<object*> *myVector;

一切都堆了.

但是,如果我使用此方法会发生什么:

However, what happens if I used this:

vector<object*> myVector;

向量本身是否存储在堆栈中?然后将其内容存储在堆中?例如,如果向量有1000万个实例,那么我是否在堆栈上有1000万个引用,而堆上有1000万个对象?还是我错过了什么?

Does the vector itself get stored on the stack? And its contents are then stored on the heap? For example, if the vector had 10 million instances, would I have 10 million references on the stack for 10 million objects on the heap? Or am I missing something?

此外,如果我使用此功能:

Moreover, if I used this:

vector<object> *myVector;

向量的内容在哪里?一切都堆起来了吗?如果是,那么它是否可以递归工作?例如:

Where do the contents of the vector go? Does everything go on the heap? If yes, then does it work recursively? For example:

vector<vector<vector<int>>> *myVector;

然后所有内部向量都存储在堆中了吗?如果是,那么在析构函数中删除myVector就足够了,而不必担心其他事情吗?

Are all the inner vectors then stored on the heap? If yes, then it should be enough deleting myVector in the destructor, without worrying on anything else?

无论如何,我假设所有适用于vector的答案也将适用于unordered_map.如果我错了,请告诉我.

Anyway, I am assuming all the answers that apply to vector would also apply to unordered_map. Please tell me if I am wrong.

提前谢谢.

推荐答案

通常,std::vector的元素将始终动态地分配在堆上.对于大多数实现,std::vector<T> foo(其中T可能是一个指针,也可能不是),堆栈上始终有三个指针来描述向量本身,而不是元素.对于std::vector<T> *bar,堆栈上有一个指针bar.三个向量指针以及向量元素都在堆上.

In general, elements of a std::vector will always be dynamically allocated on the heap. For most implementations, a std::vector<T> foo, where T could be a pointer or not, there will always be three pointers on the stack that describe the vector itself, but not the elements. For a std::vector<T> *bar, there is one pointer, bar, on stack. The three vector pointers as well as the vector elements are on the heap.

类似地,对于std::unordered_map,元素本身将始终在堆上动态分配.通常,任何具有Allocator模板参数的std类型,或包含可变数据量的任何类型,都将在堆上动态分配其数据.

Similarly, for an std::unordered_map, the elements itself will always be dynamically allocated on the heap. Generally, any std type that has a Allocator template parameter, or any type that contains a variable amount of data, will have it's data dynamically allocated on the heap.

现在有一些特殊情况,例如自定义分配器,怪异的优化,术语的特殊性,但是出于实际目的,这是您应该知道的.

Now there are special cases, like custom allocators, weird optimizations, specifics of terminology, but for practical purposes, this is what you should know.

您可能要了解的下一件事是智能指针,例如std::unique_ptrstd::shared_ptr,所有权语义和RAII.为了编写正确的代码,这实际上更重要(我需要删除和担心什么?).

The next thing that you might want to learn about, are smart pointers, e.g. std::unique_ptr, and std::shared_ptr, ownership semantics and RAII. That is actually more important to know in order to write correct code (What do I need to delete and worry about?).

这篇关于C ++使用指针指向指针向量有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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