Reputation: 2889
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.
Upvotes: 2
Views: 3676
Reputation: 20769
The sample has no leak, since the pointer you give don't refer to dynamic memory.
But is also a bad written code: string literals are constant, but C++ allow to refer them as char* to retain a C library backward compatibility. If you intend to refer to string literals, you should better use const char*
instead of char*
(in case of an attempt to modify them you got a compiler error, not a runtime exception)
Another bad thing, here, is that in a more extensive code, you sooner or later lose the control on what are the char* effectively stored in the vector: Are they granted to always be string literals or can they also be some other way allocated dynamic char[] ?? And who is responsible for their allocation / deallocation ?
std::vector says nothing about that, and if you are in the position you cannot give a clean answer to the above questions (each const char*
referred buffer can be either exist outside the vector existence scope or not), you have probably better to use std::vector<std::string>
, and treat the strings as "values" (not referenced objects), letting the string class to do the dirty job.
Upvotes: 2
Reputation: 111950
You are pushing in your vector string literals (the strings in "..."
). These aren't allocated by you. They are given to you by the C++ compiler/runtime and they have a lifetime equal to the lifetime of the app, so you can't/mustn't free them.
See for example Scope of (string) literals
Note that everything I told you was based on the fact that you are using string literals. If you need to allocate your strings' memory, then you will have to use some automatic deallocators like std::unique_ptr
(of C++11) or boost::unique_ptr
or boost::shared_ptr
(of Boost) or better use the std::string
class as suggested by Als
Upvotes: 5
Reputation: 206656
Is there a memory leak in the above C++ code?
There is no memory leak.
Since you never used new
you do not need to call delete
. You only need to deallocate dynamicmemory if it was allocated in first place.
Note that ideally, You should be using vector of std::string
.
std::vector<std::string> str;
str.push_back("String_11");
str.push_back("String_12");
str.push_back("String_13");
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.
Upvotes: 7
Reputation: 13364
There is no leak. As long as you're not making a copy of those strings, you don't need to explicitly delete or free() them.
Upvotes: 1