Consider the following C++ codes :
using namespace std;
vector<char*> aCharPointerRow;
aCharPointerRow.push_back("String_11");
aCharPointerRow.push_back("String_12");
aCharPointerRow.push_back("String_13");
for (int i=0; i<aCharPointerRow.size(); i++) {
cout << aCharPointerRow[i] << ",";
}
aCharPointerRow.clear();
After the aCharPointerRow.clear(); line, the character pointer elements in aCharPointerRow should all be removed.
Is there a memory leak in the above C++ code ? Do I need to explicitly free the memory allocated to the char* strings ? If yes, how ?
Thanks for any suggestion.
Is there a memory leak in the above C++ code?
There is no memory leak.
Since you never used
newyou do not need to calldelete. You only need to deallocate dynamicmemory if it was allocated in first place.Note that ideally, You should be using vector of
std::string.You could use std::string.c_str() in case you need to get the underlying character pointer(
char *), which lot of C api expect as an parameter.