简体   繁体   English

std :: vector - 如何释放向量中char *元素的内存?

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

Consider the following C++ codes : 请考虑以下C ++代码:

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(); aCharPointerRow.clear(); line, the character pointer elements in aCharPointerRow should all be removed. 在行中, aCharPointerRow的字符指针元素aCharPointerRow应该被删除。

Is there a memory leak in the above C++ code ? 上面的C ++代码中是否存在内存泄漏? Do I need to explicitly free the memory allocated to the char* strings ? 我是否需要显式释放分配给char *字符串的内存? If yes, how ? 如果有,怎么样?

Thanks for any suggestion. 谢谢你的任何建议。

Is there a memory leak in the above C++ code? 上面的C ++代码中是否存在内存泄漏?
There is no memory leak. 没有内存泄漏。

Since you never used new you do not need to call delete . 由于您从未使用过new ,因此无需调用delete You only need to deallocate dynamicmemory if it was allocated in first place. 如果首先分配了dynamicmemory,你只需要释放它。

Note that ideally, You should be using vector of std::string . 请注意,理想情况下,您应该使用std::string vector。

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. 您可以使用std :: string.c_str() ,以防您需要获取基础字符指针( char * ),这是C api的大部分期望作为参数。

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. 它们由C ++编译器/运行时提供给您,它们的生命周期等于应用程序的生命周期,因此您不能/不能释放它们。

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 如果你需要分配你的字符串的内存,那么你将不得不使用一些自动解除std::unique_ptr ,如std::unique_ptr (C ++ 11)或boost::unique_ptrboost::shared_ptr (Boost)或更好地使用std::string Als建议的std::string

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. 但也是一个糟糕的编写代码:字符串文字是常量,但C ++允许将它们作为char *引用以保持C库向后兼容性。 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) 如果您打算引用字符串文字,最好使用const char*而不是char* (如果尝试修改它们,则会出现编译器错误,而不是运行时异常)

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[] ?? 另一个坏处是,在更广泛的代码中,你迟早会失去对有效存储在向量中的char *的控制:它们是否被授予始终为字符串文字或者它们也可以是其他方式分配的动态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. std :: vector对此没有任何说明,如果你处于这个位置,你就无法对上述问题给出一个干净的答案(每个const char*被引用的缓冲区可以存在于向量存在范围之外),你可能更好使用std::vector<std::string> ,并将字符串视为“值”(不是引用的对象),让字符串类执行脏作业。

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. 只要您没有复制这些字符串,就不需要显式删除或释放()它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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