how to create an array of fixed length strings dynamically with following constraint
The constraint is that it should be contiguous memory. The reason is, this is being sent to another language legacy code, which expects it in that format as a 2开发者_StackOverflow中文版D array.
so esentially i want to send
char *temp[20] = { "abc", "def"};
etc where abc, def
are part of a space thats 20
byte length.
Now, i would like to dynamically create this array and add as many 20
character or less strings to it.
And then send that as an array into the different layer. Whats the best way to do this.
std::vector< std::array<char, 20> >
will do the trick
vector
allows dynamically allocating more memory for more strings, the type std::array<char,20>
ensures that each member of the vector
is indeed 20 characters (make sure to verify boundaries on copy etc, as with any array).
This is for newer C++ standard, IIRC, so older compilers might not support it. Use boost.array instead, then.
精彩评论