**关闭。**此题需要debugging details。目前不接受答复。
编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答这个问题。
2天前关闭。
Improve this question
当我在我的points 3D方法中注意到某个东西导致了分割错误时,我正在尝试做这个程序,我不知道是什么导致了它。这个程序的目标是测试出4种不同的方法,并试图得到一定的答案。我以为我基本上做对了,直到我注意到我的方法~ point 3D。虽然我解决了这个问题,我找不到任何问题,为什么会发生这种情况。
//You only need iostream, no other header files are allowed
#include <iostream>
using namespace std;
//Code for part 1
class point2D {
int x, y;
char* label;
public:
int getX(){
return x;
}
int getY(){
return y;
}
char* getLabel(){
return label;
}
void setX(int a){
x = a;
}
void setY(int b){
y = b;
}
void setLabel(char* g){
label = g;
}
virtual void show(){
cout << "The point data is " << "(" << x << "," << y << "," << label << ")" << endl;
}
point2D(int x, int y, char * label){
this->x = x;
this->y = y;
label = nullptr;
setLabel(label);
};
~point2D(){
if(label != nullptr){
delete[] label;
}
};
};
//Code for part 2
class point3D : public point2D {
protected:
int z;
public:
int getZ(){
return z;
}
void setZ(int c){
z = c;
}
void show(){
cout << "(" << getX() << "," << getY() << "," << z << "," << getLabel() << ")\n";
};
point3D(int a, int b, int c, char * l) : point2D(a,b,l) {
z = c;
};
~point3D();
};
/*
//Code for Part 3
class node {
public:
point3D* data;
node* next;
static int counter;
void show(){
if(data != nullptr){
data->show();
}
}
node(){
data = nullptr;
next = nullptr;
}
~node(){
if(data != nullptr){
delete[] data;
}
}
};
//Code for Part 4
class linkedList {
node* head;
public:
bool empty() {
return head == nullptr;
}
int size() {
int c = 0;
node* it = head;
while (it != nullptr) {
c++;
it = it->next;
}
return c;
}
bool push(point3D* np) {
node* nn = new node();
nn->data = np;
return push(nn);
}
bool push(node* nn) {
nn->next = head;
head = nn;
return true;
}
bool append(point3D* np) {
node* nn = new node();
nn->data = np;
return append(nn);
}
bool append(node* nn) {
if (head != nullptr) {
node* it = head;
while (it->next != nullptr) {
it = it->next;
}
it->next = nn;
}
else {
head = nn;
}
return true;
}
void show() {
node* it = head;
while (it != nullptr) {
it->show();
it = it->next;
}
}
linkedList() {
head = nullptr;
}
~linkedList(){
node * it = head;
node = nullptr;
while(it != nullptr){
}
}
};
int node::counter = 0;
*/
int main() {
int testMode;//Determines the test to be executed
std::cin >> testMode;
switch (testMode) {
case 1:{
//1) Test part 1
point2D * p12D = new point2D(12, 24, (char*)"test 1");
p12D->setX(25);
p12D->setLabel((char*)"Landing Zone A");
p12D->show();
delete p12D;
break;
}
case 2: {
//2) Test part 2
point3D* p13D = new point3D(12, 24, 36,(char*) "test 2");
p13D->setY(50);
p13D->setLabel((char*)"Landing Zone B");
p13D->show();
point2D* p22D = dynamic_cast<point2D*>(p13D);
p22D->show();
std::decay_t(&p13D);
break;
}
/*
case 3: {
//3) Test part 3
node* nn = new node();
std::cout << node::counter << std::endl;
delete nn;
nn = new node();
std::cout << node::counter << std::endl;
point3D* p23D = new point3D(48,60,72,"Landing Zone C");
nn->data = p23D;
nn->show();
delete nn;
break;
}
case 4: {
//4) Test part 4
linkedList* myList = new linkedList();
int i;
for (i = 0; i < 3; i++) {
node* nn = new node();
point3D* p3D = new point3D(12*i, 24*i, 36*i, "Landing Zone ");
myList->addNode(p3D);
}
myList->show();
point3D* p3D = new point3D(12 * i, 24 * i, 36 * i, "Landing Zone ");
myList->insertNode(p3D, 1);
myList->show();
delete myList;
break;
}
*/
}
return 0;
}
我尝试了很多方法,得到了一个错误:void operator delete(void*,std::size_t)' called on unallocated object ' p13 D。
1条答案
按热度按时间mwg9r5ms1#
有一个原因,你被迫作出这种明确的丑陋演员阵容。字符串文字是
const char *
。它们是由编译器创建的,并且是常量对象。在完成所有的操作之后,构造函数最终将指针存储到
label
中。析构函数很高兴地尝试
delete
一些从来不是new
ed的东西。这就是你的车祸在C中,只需要
delete
在动态范围中创建的东西,使用new
。这里没有发生这种情况,因此出现了未定义的行为和崩溃。这是C,不是C。在C++中,我们使用
std::string
来处理字符串,它可以正确地处理和管理所有的内存分配,所以你不需要担心这些低级的细节。将所有内容替换为std::string
s将完全避免整个问题。而且,作为额外的奖励,没有更多丑陋的演员阵容。