0

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

4
  • 1
    You're creating an array of vectors. I suppose thats not what you want. I have some code that uses a std::array. This is using the stack to allocate the memory. Just allocate the array on the heap. Commented Nov 30, 2020 at 13:04
  • Did you mean std::vector<uint32_t> write_data {array_size}; ? Commented Nov 30, 2020 at 13:07
  • 2
    Note that alignas(128) specified for the vector itself does not affect the alignment of its elements. If you need this, you can use a custom allocator; see, e.g., stackoverflow.com/q/12942548/580083. Commented Nov 30, 2020 at 13:10
  • I just want to basically have the same functionality of the commented out code that uses std::array but allocate memory on the heap. Can someone tell me how to do that? Thanks Commented Nov 30, 2020 at 14:13

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.