我收到错误 c++ assertion failure heap corruption detected
I am gettein an error c++ assertion failure heap corruption detected
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
但没有为新大小分配足够的内存。
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
但没有为新大小分配足够的内存。