[英]changing std::array to std::vector
I have some code that uses a std::array.我有一些使用 std::array 的代码。 This is using the stack to allocate the memory.这是使用堆栈分配 memory。 But I need to create an array of around 2MB.但我需要创建一个大约 2MB 的数组。 So doing some searching it seems that I can use std::vector that uses the heap.所以做一些搜索似乎我可以使用使用堆的 std::vector 。
I modified my code to do that我修改了我的代码来做到这一点
//alignas(128) std::array<uint32_t, array_size> write_data;
//alignas(128) std::array<uint32_t, array_size> read_data = { { 0 } };
alignas(128) std::vector<uint32_t> write_data[array_size];
alignas(128) std::vector<uint32_t> read_data[array_size];
But I am now getting error on other parts of the code.但我现在在代码的其他部分遇到错误。 This uses some threads这使用了一些线程
std::thread read_thread(read, std::ref(dev),
(void*)read_data.data(), read_data.size() * sizeof(uint32_t), dma_block_size*32);
std::thread write_thread(write, std::ref(dev),
(void*)write_data.data(), write_data.size() * sizeof(uint32_t), num_dma_blocks);
E0289 no instance of constructor "std::thread::thread" matches the argument list E0289 没有构造函数“std::thread::thread”的实例与参数列表匹配
E0153 expression must have class type E0153 表达式必须有 class 类型
The problem is on the read/write and read_data/write_data in the code above.问题出在上面代码中的 read/write 和 read_data/write_data 上。 Could someone tell me what the issue is as I am not really a cpp programmer.有人可以告诉我问题是什么,因为我不是真正的 cpp 程序员。 Thanks谢谢
You can init vector with a specific size using a constructor like this:您可以使用如下构造函数初始化具有特定大小的向量:
std::vector<int> vec(10);
By using std::vector<int> vec[10];
通过使用std::vector<int> vec[10];
you declare array of 10 vectors.你声明了 10 个向量的数组。 each vector is empty.每个向量都是空的。
BTW: if you use std::vector<int> vec{10};
顺便说一句:如果你使用std::vector<int> vec{10};
you declare a vector with a single int
which is equal to 10.您声明一个具有等于 10 的单个int
的向量。
It seems if I change the vector init to the below then it works似乎如果我将向量初始化更改为以下,那么它就可以工作
alignas(128) std::vector<uint32_t> write_data (array_size);
alignas(128) std::vector<uint32_t> read_data (array_size);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.