Timeline for Create a C style char** from a C++ vector<string>
Current License: CC BY-SA 4.0
19 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Mar 20, 2023 at 22:33 | comment | added | Walter Nissen |
An elegant solution that both re-uses the storage of the std::strings and takes care of the memory of the "array" itself with the std::vector .
|
|
Oct 12, 2018 at 12:36 | history | edited | Useless | CC BY-SA 4.0 |
added 642 characters in body
|
Oct 12, 2018 at 11:26 | comment | added | Lightness Races in Orbit |
This is probably how I'd do it too, though I do try to avoid things like execve() .
|
|
Oct 12, 2018 at 7:22 | comment | added | Toby Speight |
@Headcrab data() has const and non-const overloads, since C++17. const CharT* data() const and CharT* data() .
|
|
Oct 12, 2018 at 6:32 | comment | added | Headcrab |
@Toby Speight, doesn't data() also return a pointer to const char ? The docs say that "both string::data and string::c_str are synonyms and return the same value".
|
|
Oct 11, 2018 at 20:33 | history | edited | Useless | CC BY-SA 4.0 |
added 1 character in body
|
Oct 10, 2018 at 19:10 | comment | added | ncalmbeblpaicr0011 | This is how I would do it as well. There's no reason to create a copy of all the data. | |
Oct 10, 2018 at 17:16 | comment | added | Toby Speight |
@nurettin, the null pointer is there because the users need it to delimit the array (it's the interface of exec() family of functions).
|
|
Oct 10, 2018 at 17:10 | comment | added | nurettin |
Also, why force yourself to use transform and back_inserter when a simple for(auto &i: input){ result.push_back(i.data()); } will do. Hoping there will be implementation specific magic in std::transform that somehow works in conjunction with std::back_inserter ?
|
|
Oct 10, 2018 at 17:09 | comment | added | GuyRT |
@TobySpeight - Ah - didn't know that, thanks. I now notice that c++11 also guarantees that data will do the right thing for an empty string.
|
|
Oct 10, 2018 at 17:07 | comment | added | nurettin | This should be the answer. No manual memory management. However, I'm not sure why you added the last nullptr at the end of your vector of char pointers. | |
Oct 10, 2018 at 17:01 | comment | added | Useless | Yeah, there was always an argument that an implementation could do something different, but I've never seen one that did. And hopefully we're all on at least C++11 now. | |
Oct 10, 2018 at 16:56 | comment | added | Martin Bonner supports Monica |
… and all known implementations null-terminated the result of data() before C++11.
|
|
Oct 10, 2018 at 16:46 | comment | added | Toby Speight |
@GuyRT, c_str() returns a pointer to const char, so it would need a const_cast . The result of data() is guaranteed to be null-terminated since C++11.
|
|
Oct 10, 2018 at 16:42 | comment | added | GuyRT |
Oh - another thing. I think you should use c_str() instead of data() (which isn't guaranteed to null terminate).
|
|
Oct 10, 2018 at 16:38 | history | edited | Useless | CC BY-SA 4.0 |
added 86 characters in body
|
Oct 10, 2018 at 16:36 | comment | added | Useless | Good points both! | |
Oct 10, 2018 at 16:06 | comment | added | GuyRT |
I much prefer this approach. One minor point - I think you need to push_back a nullptr at the end. Also - if you go down the class path, you'll need to disable or do the right thing for copy construction and assignment.
|
|
Oct 10, 2018 at 15:02 | history | answered | Useless | CC BY-SA 4.0 |