From 09b04e4bd6abd80e9002482ac7c209a64f94e069 Mon Sep 17 00:00:00 2001 From: neingeist Date: Sat, 23 Aug 2014 11:19:16 +0200 Subject: [PATCH] no need to copy the strings, just reference them --- ohai-const-strings.cpp | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/ohai-const-strings.cpp b/ohai-const-strings.cpp index d56c155..74515ac 100644 --- a/ohai-const-strings.cpp +++ b/ohai-const-strings.cpp @@ -20,10 +20,9 @@ const char** convert_to_c(const std::vector strings) { // Note: perfectly fine to "new" const char*. const char** strings_c = new const char*[strings.size()+1]; for (size_t i = 0; i < strings.size(); ++i) { - // XXX No need to copy the string here. - char* string_c = new char[strings.at(i).size()+1]; - strncpy(string_c, strings.at(i).c_str(), strings.at(i).size()+1); - strings_c[i] = string_c; + // Note: const char** = non-const "array" to const char*, i.e. can modify + // pointers to the strings but not the strings themselves. + strings_c[i] = strings.at(i).c_str(); } strings_c[strings.size()] = NULL; @@ -31,9 +30,6 @@ const char** convert_to_c(const std::vector strings) { } void free_c(const char** strings_c) { - for (size_t i = 0; strings_c[i] != NULL; ++i) { - delete[] strings_c[i]; - } delete[] strings_c; }