我收到以下错误
" HEAP CORRUPTION DETECTED:在正常块(#271)之后的0x0108C4A8。 CRT检测到应用程序在堆缓冲区结束后写入内存。"
背景信息
我试图模仿继承,多态和动态方法绑定 在c ++中没有实际使用这些功能。我有一个模拟一个名为' Square_new(x,y,sidelen)'的构造函数的函数。调用new并返回Square指针。然后我将其转换为父类" Shape"。我知道这段代码可以运行
cout << "TESTING SQUARE" << endl;
cout << "Initializing Square at x=1,y=1,sidelength=5" << endl;
Shape* s2 = (Shape*)Square_new(1, 1, 5);
cout << "Expected area = 5*5 = 25" << endl << endl;
// later on in the code
delete s2;
但是当我添加以下代码时(当然是删除调用之上)
//Added Portion
Rectangle* r2 = (Rectangle*)s2;
Square* sq = (Square*)s2;
这是Square.cpp的实现
void** vmtSquare;
Square* Square_new(double positionX, double positionY, double sidelength) {
Square* s = new Square;
Square_Square(s, positionX, positionY, sidelength);
s->vmt = vmtSquare;
return s;
}
void Square_Square(Square* _this, double positionX, double positionY, double sidelength) {
Rectange_Rectangle((Rectangle*)_this, positionX, positionY, sidelength, sidelength);
Square_createVMT;
}
// This resize will ovveride the rectangle resize
void Square_resizeA(Rectangle* _this, double width, double height) {
if (width == height) {
Rectangle_resize(_this, width, height);
}
}
// following two functions are extensions
void Square_resizeB(Square* _this, double sidelength) {
Rectangle_resize((Rectangle*)_this, sidelength, sidelength);
}
double Square_getSideLength(Square* _this) {
return Rectangle_getWidth((Rectangle*)_this);
}
void Square_Destructor(Square* _this) {
// The function doesnt do any thing but call
// parent destructor (Rectangle)
Rectangle_Destructor((Rectangle*)_this);
}
void Square_createVMT() {
// copy Rectangle vmt
vmtSquare = new void*[10];
vmtSquare[0] = (void*)Shape_getPositionX;
vmtSquare[1] = (void*)Shape_getPositionY;
vmtSquare[2] = (void*)Shape_move;
vmtSquare[3] = (void*)Rectangle_area;
vmtSquare[4] = (void*)Rectangle_Destructor;
vmtSquare[5] = (void*)Rectangle_getWidth;
vmtSquare[6] = (void*)Rectangle_getHeight;
//override rectangle resize and destructor
vmtSquare[4] = (void*)Square_Destructor;
vmtSquare[7] = (void*)Square_resizeA;
//extension methods
vmtSquare[8] = (void*)Square_resizeB;
vmtSquare[9] = (void*)Square_getSideLength;
}
反过来的广场来源于&#39;来自矩形,来自Shape
// Rectangle.cpp
// Assignment 4 example
#include "Rectangle.h"
#include "Shape.h"
void** vmtRectangle;
Rectangle* Rectangle_new(double positionX, double positionY, double width, double height) {
Rectangle* r = new Rectangle;
Rectange_Rectangle(r, positionX, positionY, width, height);
r->vmt = vmtRectangle;
return r;
}
void Rectange_Rectangle(Rectangle* _this, double positionX, double positionY, double width, double height) {
//set fields for this Rectangle
_this->width = width;
_this->height = height;
Shape_Shape((Shape*)_this, positionX, positionY);
Rectangle_createVMT();
}
double Rectangle_getWidth(Rectangle*_this) {
return _this->width;
}
double Rectangle_getHeight(Rectangle* _this) {
return _this->height;
}
void Rectangle_resize(Rectangle* _this, double width, double height) {
_this->width = width;
_this->height = height;
}
double Rectangle_area(Shape* _this) {
Rectangle* r = (Rectangle*)_this;
return r->height * r->width;
}
void Rectangle_Destructor(Rectangle* _this) {
//doesnt do anything accept call parent's destructor
Shape_Destructor((Shape*)_this);
}
void Rectangle_createVMT() {
vmtRectangle = new void*[8];
//copy shapes vmt
vmtRectangle[0] = (void*)Shape_getPositionX;
vmtRectangle[1] = (void*)Shape_getPositionY;
vmtRectangle[2] = (void*)Shape_move;
vmtRectangle[3] = (void*)0;
vmtRectangle[4] = (void*)Shape_Destructor;
//overwrite area and destructor
vmtRectangle[3] = (void*)Rectangle_area;
vmtRectangle[4] = (void*)Rectangle_Destructor;
//extensions
vmtRectangle[5] = (void*)Rectangle_getWidth;
vmtRectangle[6] = (void*)Rectangle_getHeight;
vmtRectangle[7] = (void*)Rectangle_resize;
}
这是Rectangle的父级
// Shape.cpp
// Assignment 4 example
// Contains the definitions needed to emulate the Shape "class".
#include "Shape.h"
void Shape_Shape(Shape* _this, double positionX, double positionY)
{
_this->positionX = positionX;
_this->positionY = positionY;
}
double Shape_getPositionX(Shape* _this)
{
return _this->positionX;
}
double Shape_getPositionY(Shape* _this)
{
return _this->positionY;
}
void Shape_move(Shape* _this, double positionX, double positionY)
{
_this->positionX = positionX;
_this->positionY = positionY;
}
void Shape_Destructor(Shape* _this) {
//this function does absolutely nothing
//and is here for emulation purposes
}
void Shape_createVMT()
{
vmtShape = new void*[5];
vmtShape[0] = (void*) Shape_getPositionX;
vmtShape[1] = (void*) Shape_getPositionY;
vmtShape[2] = (void*) Shape_move;
// area() is abstract, so its entry in the VMT should be 0 (NULL).
vmtShape[3] = (void*) 0;
vmtShape[4] = Shape_Destructor;
}
答案 0 :(得分:1)
事实证明这是一个超级模糊的错误。结构被认为用于表示类。构造Square时,它调用矩形构造函数。但是为了使其工作,square必须具有与矩形完全相同的字段。我没有在代码中发布这个,所以没有人能帮助我(抱歉浪费你的时间),但无论如何现在它已经
最初位于Square.h和Rectangle.h
之内struct Square {
void** vmt;
//shape
double positionX;
double positionY;
};
struct Rectangle {
void** vmt;
//shape
double positionX;
double positionY;
//rectangle
double width;
double height;
};
当Square应定义如下
struct Square {
void** vmt;
//shape
double positionX;
double positionY;
//rectangle
double width;
double height;
};
我仍然不知道为什么这甚至是重要的,如果有人想要和解释我会有兴趣知道