使用std :: vectors(损坏的大小与prev_size)可能的指针无效
我正在构建一个C ++应用,该应用需要创建一个必须指向原始内存地址的结构数组(void *+ size_t)。为了保留堆栈上的值(std :: vector< uint8_t>),我正在使用另一个标准向量(std :: vectorstd :: vectorstd :: vector< uint8_t>)。通过这种方法,我会得到一个例外(损坏的大小与prev_size)。我认为数据()指针正在无效。
我创建了与我的代码相似的示例。如果您可以让我知道错误和/或告诉我另一种替代方案,以将数据保留在范围上,那将是很棒的。
#include <vector>
#include <unistd.h>
#include <stdint.h>
int fill_vector(std::vector<uint8_t> &t_vec)
{
for (int i = 0; i < 10; i++)
{
// Here a value is copied (this is an example)
t_vec.push_back(0x01);
}
}
struct SContainer
{
void *data;
size_t size;
}
int main(int argc, char const *argv[])
{
std::vector<std::vector<uint8_t>> t_vec_vals;
std::vector<uint8_t> t_vec_val;
std::vector<SContainer> t_vec_containers;
for (int i = 0; i < 10; i++)
{
fill_vector(t_vec_val);
// Keep filled vector on the scope
t_vec_vals.push_back(t_vec_val);
SContainer c;
c.data = t_vec_vals[i].data();
c.size = t_vec_vals[i].size();
t_vec_containers.push_back(c);
}
// Use t_vec_containers data
// Exception:
// corrupted size vs. prev_size
}
有什么问题?
I am building a C++ app that needs to create an array of structures that must point to raw memory addresses (void * + size_t). In order to keep the values (std::vector<uint8_t>) on the stack I am using another standard vector (std::vectorstd::vector<uint8_t>). With this approach I am getting an exception (corrupted size vs. prev_size). I think the the data() pointer is being invalidated.
I create this example that is similar to my code. If you could let me known the error and/or tell me another alternative to keep the data on the scope it will be awesome.
#include <vector>
#include <unistd.h>
#include <stdint.h>
int fill_vector(std::vector<uint8_t> &t_vec)
{
for (int i = 0; i < 10; i++)
{
// Here a value is copied (this is an example)
t_vec.push_back(0x01);
}
}
struct SContainer
{
void *data;
size_t size;
}
int main(int argc, char const *argv[])
{
std::vector<std::vector<uint8_t>> t_vec_vals;
std::vector<uint8_t> t_vec_val;
std::vector<SContainer> t_vec_containers;
for (int i = 0; i < 10; i++)
{
fill_vector(t_vec_val);
// Keep filled vector on the scope
t_vec_vals.push_back(t_vec_val);
SContainer c;
c.data = t_vec_vals[i].data();
c.size = t_vec_vals[i].size();
t_vec_containers.push_back(c);
}
// Use t_vec_containers data
// Exception:
// corrupted size vs. prev_size
}
What's the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,向量的一个问题是所有存储在1连续块中。每次调整大小时,存储都重新分配,并且很可能会移动。
因此,一旦将进一步的元素推入其中,T_VEC_VALS中第一个元素的地址不太可能成为安全价值。
如果要使用t_vec_val的std ::列表,则单独分配单个元素,并且直到删除才移动。
另一个选择是将矢量保留到您期望使用的大小。
So one problem with vector is all the storage is in 1 continuous block. Each time you resize it, the storage is reallocated and is very likely to move.
Therefore, the address of the first element in t_vec_vals is unlikely to be a safe value, once further elements are pushed into it.
If you were to use a std::list for t_vec_val, then the individual elements are individually allocated, and do not move until deleted.
Another option is to reserve the vector to the size you expect to use.