C++ delete [] char *,内存问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1408600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
delete [] char *, memory issues
提问by David Andres
I have a global pointer variable
我有一个全局指针变量
char* pointer = new char[500];
/* some operations... */
there is a seperate FreeGlobal() function that does free up the pointer as below:
有一个单独的 FreeGlobal() 函数可以释放指针,如下所示:
delete[] pointer;
First time when the function is called, it actually frees up the memory and now the pointer is a bad pointer. But when we call this more than once, it throws an exception.
第一次调用该函数时,它实际上释放了内存,现在该指针是一个坏指针。但是当我们多次调用它时,它会抛出异常。
Is there a way to check the pointer variable before calling delete [] again? What are the work arounds? Is this a bad practice?
有没有办法在再次调用 delete [] 之前检查指针变量?有哪些解决方法?这是一种不好的做法吗?
Thank you.
谢谢你。
回答by David Andres
Set pointer to null after you delete it. You should not try to delete the same data more than once.
删除后将指针设置为空。您不应尝试多次删除相同的数据。
As mentioned by GRB in the comments for this post, it is perfectly safe to call delete[] NULL
.
正如 GRB 在这篇文章的评论中提到的,调用delete[] NULL
.
回答by GManNickG
I would suggest you actually fix your code. Double deleting something is a terrible thing to do. Rather than make hackish routines to let this happen, fix the real problem: no more double deletes.
我建议你实际修复你的代码。双重删除某些东西是一件可怕的事情。与其制定一些骇人听闻的例程来让这种情况发生,不如解决真正的问题:不再进行双重删除。
Find out why you're deleting something twice, and stop it from happening. This would probably be easier if you weren't using global variables. If you need a global resource, use a singleton.
找出为什么要两次删除某些内容,并阻止它发生。如果您不使用全局变量,这可能会更容易。如果您需要全局资源,请使用单例。
Additionally, use a std::vector<char>
, or boost::array<char, 100>
, so you don't need to worry about memory. You can't accidentally delete something twice if you don't have to worry about (or access to) deleting it.
此外,使用std::vector<char>
, 或boost::array<char, 100>
,因此您无需担心内存。如果您不必担心(或访问)删除某个内容,您就不会意外删除它两次。
回答by Michael Kristofik
delete
on a bad pointer results in Undefined Behavior. You're lucky that it threw an exception, but you certainly shouldn't rely on that happening. To prevent it, simply set the pointer to 0 after the delete
. It's safe to delete
a null pointer (in case FreeGlobal()
gets called more than once), so you don't have to do any if-test.
delete
在错误的指针上导致未定义的行为。你很幸运它抛出了一个异常,但你当然不应该依赖这种情况的发生。为了防止它,只需将指针设置为 0 后delete
。delete
空指针是安全的(以防FreeGlobal()
被多次调用),因此您不必进行任何 if 测试。
void FreeGlobal()
{
delete [] pointer;
pointer = 0;
}
回答by jmucchiello
You don't need to check if the pointer is null before calling delete or delete[]. Your function should look like this:
在调用 delete 或 delete[] 之前,您不需要检查指针是否为空。您的函数应如下所示:
void freeGlobal(char*& ptr) {
delete[] ptr;
ptr = 0;
}
What I want to know is why this thing is global and not inside a class?
我想知道的是为什么这个东西是全局的而不是在一个类中?
class buffer {
char* buf;
size_t size;
public:
buffer(size_t n) : size(n), buf(new char[size]) {}
~buffer() { delete[] buf; buf = 0; }
operator char*() { return buf; }
char& operator[](size_t ofs) {
assert(ofs >= 0 && ofs < size);
return buf[ofs];
}
};
There are probably a couple things wrong with my implementation as I just typing it in here. Another question is why you aren't using the std::string for this char buffer?
我的实现可能有一些问题,因为我只是在这里输入它。另一个问题是你为什么不使用 std::string 这个字符缓冲区?
回答by KFro
Check this link out at the CERT Coding Standard Site. After freeing a variable you should set the pointer back to NULL since its not really pointing to anything anymore.
在CERT 编码标准站点上查看此链接。释放变量后,您应该将指针设置回 NULL,因为它不再真正指向任何东西。
回答by jkeys
Your freeGlobal() function should make it point to 0, so define the function as such:
您的 freeGlobal() 函数应使其指向 0,因此将函数定义为:
//if ptr is not null, delete; otherwise, return
void freeGlobal(char*& ptr)
{
if (ptr != 0)
{
delete[] ptr;
ptr = 0;
}
}
Edit: 0 == NULL, at least for now, in C++.
编辑:0 == NULL,至少现在,在 C++ 中。
回答by Havenard
Pointers pointing to nowhere, should point to nowhere. pointer = null;
指向无处的指针,应该指向无处。 pointer = null;
if (pointer != null) {
delete[] pointer;
pointer = null;
}