memset works with bytes, so it populates your array of int values 0x0101010101 (assuming int is 32 bits), which is decimal 16843009.
If you need to populate a 2-dimensional C-style array with a number:
int l[3][3]; std::fill_n(*l, sizeof l / sizeof **l, 1);
*l here unpacks int[3][3] into a pointer to the first element of the array ( int* ), sizeof l / sizeof **l gives the number of elements in the array.
It uses the C ++ requirement that arrays be adjacent in memory without spaces, so multidimensional arrays have the same layout as unidimensional ones. For instance. int [3][3] has the same layout as int[3 * 3] .
And, unlike memset , std::fill_n works on an object level, not on bytes. For built-in types, the optimized version usually includes SIMD instructions that are no less efficient than memset .
source share