简体   繁体   中英

changing std::array to std::vector

I have some code that uses a std::array. This is using the stack to allocate the memory. But I need to create an array of around 2MB. So doing some searching it seems that I can use std::vector that uses the heap.

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
E0153 expression must have class type

The problem is on the read/write and read_data/write_data in the code above. Could someone tell me what the issue is as I am not really a cpp programmer. 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]; you declare array of 10 vectors. each vector is empty.

BTW: if you use std::vector<int> vec{10}; you declare a vector with a single int which is equal to 10.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM