C++使用vector< vector>用连续数据缓冲区表示矩阵
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<vector<float>> func(int M)
{
// res = matrix size MxM
vector<vector<float>> res;
float* buffer = static_cast<float*>(malloc(M * M * sizeof(float)));
res.reserve(M);
for (int i=0; i<M; i++) {
res.emplace_back(buffer + i * M, buffer + (i + 1) * M);
/// res[i] = compute_the_matrix();
}
return res;
}
我需要制作一个使用vector&lt; vector&lt; float&gt;&gt;
表示矩阵的函数。但是,这是低效的,因为行可能位于内存中的不同位置,而良好的矩阵应该在连续块中具有其所有元素。
为此,i malloc
连续的内存块。然后从该块初始化向量。
这种方法是否安全?矢量被破坏时会正确自由记忆吗?我能想到的另一种情况是,如果res [i] = Compute_the_matrix();
中有例外,那么我们有一个内存泄漏。
编辑:我认为此代码执行复制构建器,而不是移动构造者,因此这不是我想要的。那么,如何制作一个在内存中连续的向量呢?
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<vector<float>> func(int M)
{
// res = matrix size MxM
vector<vector<float>> res;
float* buffer = static_cast<float*>(malloc(M * M * sizeof(float)));
res.reserve(M);
for (int i=0; i<M; i++) {
res.emplace_back(buffer + i * M, buffer + (i + 1) * M);
/// res[i] = compute_the_matrix();
}
return res;
}
I'm required to make a function that use vector<vector<float>>
to represent a matrix. However, it's inefficient because the rows might be at different location in memory, while a good matrix should have all its element in a continuous block.
To do this, I malloc
a continuous block of memory. Then initialize the vectors from this block.
Is this method safe and will the vectors free memory correctly when it's destructed? Another situation I can think of is if there's an exception in res[i] = compute_the_matrix();
, then we have a memory leak.
Edit: I think this code perform copy-constructor instead of move-constructor, so it's not what I'm looking for. So, how can I make a vector that is continuous in memory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该代码无法完成您认为的作用。
该行
创建了一个新的
std :: vector&lt; float&gt;
以添加到res
。此std :: vector&lt; float&gt;
将分配自己的内存,以将数据的副本保存在范围内[buffer + i * m,buffer +(i + 1) * m)
,这也会导致不确定的行为,因为您从未在此范围内初始化数据。因此,最后,您根本没有使用
malloc
的内存来为向量。该内存只是在功能末尾泄漏。您无法指定
vector&lt; vector&lt; float&gt;&gt;
完全应该使用。根本无法修改其分配策略。您能做的就是etiher使用vector&lt; float&gt;
而不是将矩阵条目保持在单个矢量中线性索引,或者您可以使用vector&lt; vector&lt; vector&lt; float,alloc1&gt; gt; gt;,gt;,gt; gt; gt; gt; gt; gt;
其中alloc1
和alloc2
是某些自定义分配器类型,您以某种方式指定了分配行为,以便存储布局更接近您想要的(尽管我怀疑我怀疑)后者可以在这里做得很好,也可以努力仅使用线性表示)。The code doesn't do what you think it does.
The line
creates a new
std::vector<float>
to add tores
. Thisstd::vector<float>
will allocate its own memory to hold a copy of the data in the range[buffer + i * M, buffer + (i + 1) * M)
, which also causes undefined behavior because you never initialized the data in this range.So, in the end you are not using the memory you obtained with
malloc
at all for the vectors. That memory is simply leaked at the end of the function.You can't specify what memory a
vector<vector<float>>
should use at all. There is simply no way to modify its allocation strategy. What you can do is etiher use avector<float>
instead to hold the matrix entries linearly indexed in a single vector or you can use avector<vector<float, Alloc1>, Alloc2>
whereAlloc1
andAlloc2
are some custom allocator types for which you somehow specify the allocation behavior so that the storage layout is closer to what you want (although I doubt that the latter can be done nicely here or is worth the effort over just using the linear representation).