堆损坏在VS中检测到错误,但在其他IDE中工作良好
Heap corruption detected error in VS, but working fine with other IDE
我正在使用VoidPtr做一些事情,当我在其他IDE(如Quincy或Eclipse)上测试我的代码时,我运行它们没有任何问题。
然而,当我试图在Visual Studio 2015中运行我的代码时,显示很好,但我偶然发现了一个名为
的错误HEAP corrupt DETECTED: after Normal block #138 at (some address)
我无法找到错误的位置,因为它显示了一个指针的地址,这对我来说更难调试。
我正在尝试联合两个void指针数组的数据。
void unionAnimalArray(VoidPtr* animalArray, int size, VoidPtr* animalArray2, int size2)
{
int sizeu;
VoidPtr *vpArray = &animalArray[0];
VoidPtr *vpArray2 = &animalArray2[0];
VoidPtr *end = &animalArray2[size2];
VoidPtr *su = new VoidPtr[12];
su = animalArray;
sizeu = size;
VoidPtr tempu;
bool check;
while (vpArray2 != end)
{
do
{
tempu = *vpArray2;
check = true;
for (int j = 0; j<size; j++)
{
if (j == 0)
vpArray = &animalArray[0];
if (*(static_cast<Animal*>(tempu)) == *(static_cast<Animal*>(*vpArray)))
{
check = false;
break;
}
++vpArray;
}
if (!check)
++vpArray2;
} while (!check && vpArray2 != end);
if (vpArray2 != end)
{
vpArray = &su[sizeu];
*vpArray = tempu;
++sizeu;
++vpArray2;
}
}
cout << "The union is" << endl;
cout << "t{";
printAnimalArray(su, sizeu);
delete[]su;
}
void unionAnimalArray(VoidPtr* animalArray, int size, VoidPtr* animalArray2, int size2)
...
VoidPtr *su = new VoidPtr[12]; // su points to new memory
su = animalArray; // su now points to same memory as animalArray2
...
delete[]su; // deletes memory shared with animalArray2
数组应该按元素复制而不是赋值:
for(int i = 0; i < size2; ++i) {
su[i] = animalArray[i];
}
你可以在自动内存中保存缓冲区su
,因为你知道在编译时缓冲区的大小:
VoidPtr su[12];
考虑一下:
VoidPtr *su = new VoidPtr[12];
su = animalArray;
12个新的VoidPtr-s去哪里了?谁和何时将被删除?
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- C++:如何在CLion IDE中安装Boost
- GL_SHADERSTORAGE_BUFFER位置是否与其他着色器位置冲突
- 为什么我不能在 C++ 中的特定函数重载中调用同一函数的任何其他重载?
- 在其他文件中创建类时在 c++ 项目中不起作用
- 类与私有变量的其他类之间的线程安全性
- 将--whole archive链接器选项与CMake和具有其他库依赖项的库一起使用
- GlobalAlloc而不是其他分配方法
- C++从其他 constexpr 创建 lambda 不能按顺序执行 Constexpr
- 断言中的Fold表达式在某些计算机上编译,但在其他计算机上不编译
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- 结构和双指针隐藏在其他结构中,多层混淆
- C++/VIM相对于其他IDE的怪异EOF行为
- CLion(和其他IDE?)的"#pragma ide diagnostic ignored"列表
- Xcode 6 的 Playground 功能适用于 Visual Studio 或其他使用 C++ 的 IDE?
- 线程c++11标准和Netbeans/其他IDE
- 堆损坏在VS中检测到错误,但在其他IDE中工作良好
- 在其他操作系统中搜索ios的IDE