c++ - 我收到错误 c++ assertion failure heap corruption detected

标签 c++ heap-corruption

when i run this code i get an error during the destructor any ideas? the data members of FloatArray are.... float* mData; int mSize;

我得到的错误是 HEAP CORRUPTION DETECTED: after normal block (#141) at 0x004c7db8

#ifndef FLOAT_ARRAY_H
#define FLOAT_ARRAY_H
class FloatArray
{
public:
    // Create a FloatArray with zero elements.
    FloatArray();

    // Create a FloatArray with 'size' elements.
    FloatArray(int size);

    // Create a FloatArray from another FloatArray--
    // be sure to prevent memory leaks!
    FloatArray(const FloatArray& rhs);

    // Free dynamic memory.
    ~FloatArray();

    // Define how a FloatArray shall be assigned to
    // another FloatArray--be sure to prevent memory
    // leaks!
    FloatArray& operator=(const FloatArray& rhs);

    // Resize the FloatArray to a new size.
    void resize(int newSize);

    // Return the number of elements in the array.
    int size();

    // Overload bracket operator so client can index
    // into FloatArray objects and access the elements.
    float& operator[](int i);

private:
    float* mData; // Pointer to array of floats (dynamic memory).
    int mSize; // The number of elements in the array.
};
#endif // FLOAT_ARRAY_H


#include "FloatArray.h"

FloatArray::FloatArray()
{
    mData = new float[0];
}

FloatArray::FloatArray(int size)
{
    mData = new float[size];
    mSize = size;
}

FloatArray::FloatArray(const FloatArray& rhs)
{
    mData = new float[rhs.mSize];
    mSize = rhs.mSize;

    for (int i = 0; i < rhs.mSize; i++)
    {
        mData[i] = rhs.mData[i];
    }

}

FloatArray::~FloatArray()
{
    delete[] mData;
    mData = 0;
}

FloatArray& FloatArray::operator=(const FloatArray& rhs)
{
    if (this == &rhs)
        return *this;

    delete[] mData;

    mData = new float[rhs.mSize];
    mSize = rhs.mSize;

    for (int i = 0; i < rhs.mSize; i++)
    {
        mData[i] = rhs.mData[i];
    }

    return *this;
}

void FloatArray::resize(int newSize)
{
    mSize = newSize;
}

int FloatArray::size()
{
    return mSize;
}

float& FloatArray::operator[](int i)
{
    return mData[i];
}

#include "FloatArray.h"
#include <iostream>
using namespace std;
void PrintFloatArray(FloatArray& fa)
{
    cout << "{ ";
    for (int i = 0; i < fa.size(); ++i)
        cout << fa[i] << " ";
    cout << "}" << endl << endl;
}
int main()
{
    FloatArray A;
    A.resize(4);
    A[0] = 1.0f;
    A[1] = 2.0f;
    A[2] = 3.0f;
    A[3] = 4.0f;
    cout << "Printing A: ";
    PrintFloatArray(A);
    FloatArray B(A);
    cout << "Printing B: ";
    PrintFloatArray(B);
    FloatArray C = B = A;
    cout << "Printing C: ";
    PrintFloatArray(C);
    A = A = A = A;
    cout << "Printing A: ";
    PrintFloatArray(A);
}

最佳答案

您没有在默认构造函数 FloatArray::FloatArray() 中将 mSize 初始化为 0。

此外,在 resize() 中,您仅更改了 mSize,但没有为新大小分配足够的内存。

关于c++ - 我收到错误 c++ assertion failure heap corruption detected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30043649/

相关文章:

c++ - 字符串堆损坏

qt - 尝试删除Qimage Format_RGB888时Qt应用程序崩溃

C++ 在 priority_queue 中使用 std::greater() 并排序

c++ - 为我的应用程序第 2 部分保存用户数据

c++ - 使用 C++ 和 Qt 如何将 16 位原始文件显示为图像?

c++ - 将实用程序放在与其使用的类型相同的 namespace 中?

android - 无法在android中启动cocos2dx ios项目

java - 将 DLL 与 JNA 一起使用时 C 中的堆损坏

c - 堆损坏错误

c++ - 将两个字节变为短字节时堆损坏。 C++