1

Is there a memset or memcpy operation to set a single int16, or int32 to an entire array?

Basically I have an integer array and I want to broadcast a single value to all of its elements. memset only supports setting a 1-byte char.

phuclv
  • 37,963
  • 15
  • 156
  • 475
M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118
  • 6
    [`std::fill`](https://en.cppreference.com/w/cpp/algorithm/fill) + compiler optimizations. – 463035818_is_not_an_ai Jan 17 '23 at 10:32
  • @463035818_is_not_a_number thanks for the answer, (mind to post as answer?) – M.kazem Akhgary Jan 17 '23 at 10:38
  • I think its not an answer, but just a comment. I mean its just a fancy way of saying "Write a loop and call it a day" – 463035818_is_not_an_ai Jan 17 '23 at 10:40
  • If you are dynamically allocating the memory you can also change to `std::vector values(size,initial_value);` [std::vector constructor 3](https://en.cppreference.com/w/cpp/container/vector/vector). Or you can use [std::array's fill method](https://en.cppreference.com/w/cpp/container/array/fill) – Pepijn Kramer Jan 17 '23 at 12:12
  • It would help if you could show some of your current code that you want to change. – Pepijn Kramer Jan 17 '23 at 12:13
  • 1
    duplicates: [Can memset be used for multi-byte types and non-zero values](https://stackoverflow.com/q/42578790/995714), [Initialization of all elements of an array to one default value in C++?](https://stackoverflow.com/q/1065774/995714), [C++ memset() for long (64bit) types (duplicate)](https://stackoverflow.com/q/7893217/995714) – phuclv Jan 17 '23 at 14:46

0 Answers0