简体   繁体   中英

std::vector - how to free the memory of char* elements in a vector?

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 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.

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM