C++ 指针 "error: double free or corruption (out)"

标签 c++ pointers

这是我的代码:

uint16_t * ptemparr = new uint16_t[20];
for (int x=0;x<2;x++)
{
   function(ptemparr);
   ptemparr ++;

}
delete[] ptemparr;

当我这样做时,我得到了这个错误:

double free or corruption (out)

编辑: 谢谢,我明白了为什么会出现此错误,现在您认为这是一个更好的主意吗?

uint16_t temparr[20];
uint16_t * ptemparr = temparr;
for (int x=0;x<2;x++)
{
   function(ptemparr);
   ptemparr ++;

}

这样我就在堆栈上创建了指针并且没有内存泄漏问题。 此外,上面的代码必须每 1 秒运行一次,所以在让我知道什么是这种情况下的最佳编码实践之前,请牢记这一点

最佳答案

您需要将相同的地址传递给由 new [] 返回的 delete []
此外,确保 function() 不会通过对传递的指针调用delete`来释放内存。

关于C++ 指针 "error: double free or corruption (out)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9901339/

相关文章:

c++ - 将这种原始指针情况变成 unique_ptr?

pointers - 空结构 slice 的地址

c++ - 指针麻烦

c - C语言中指针操作的理解问题

c - 覆盖函数指针的实现并用于共享库

c++ - 如何查看内存是否已经初始化?

c++ - c++中using是什么意思?

c++ - C++ 教程中的 2D 游戏开发

C++ 将指针数组传递给函数指针数组时出现问题

c++ - 寻求简单内存数据库*服务器*的建议(不需要持久性)