gpt4 book ai didi

c++ - 深层复制构造函数 - 类 C++ 中的动态数组无效的 free()/delete/delete[]/realloc()

转载 作者:太空宇宙 更新时间:2023-11-04 15:35:37 24 4
gpt4 key购买 nike

我想写一个简单的类来表示一个点 A(x,y)。但是,我在复制构造函数方面遇到了一些问题。当我尝试使用 valigrind 分析我的代码时,它给我 invalid free()/delete/delete[]/realloc() 错误。请看一看:

#include <iostream>
#include <cmath>
using namespace std;

class Point
{
private :
int *coord;
public:
Point();
Point(int x, int y);
Point(const Point& p);
~Point();
void printpoint();

};

Point::Point()
{
coord = new int[2];
coord[0] = 0;
coord[1] = 0;
}

Point::Point(int x, int y)
{
coord = new int[2];
coord[0] = x;
coord[1] = y;
}

Point::Point(const Point& p)
{
if (this != &p)
{
if(coord != NULL)
{
delete[] coord;
coord = NULL;
}
coord = new int[2];
coord[0] = p.coord[0];
coord[1] = p.coord[1];
}
}

Point::~Point()
{
if(coord != NULL)
{
delete[] coord;
coord = NULL;
}
}

void Point::printpoint()
{
std::cout << "Point (" << coord[0] << "," << coord[1] << ")\n";
}


int main()
{
Point p1;
p1.printpoint();

Point *p2 = new Point();
p2->printpoint();

Point p3(3,4);
p3.printpoint();

Point *p4 = new Point(6,7);
p4->printpoint();

Point *p5 = &p1;
p5->printpoint();

Point *p6 = p4;
p6->printpoint();

Point *p7 = new Point(p3);
p7->printpoint();

Point p8 = Point(p3);
p8.printpoint();

Point p9 = p1;
p9.printpoint();

Point p10 = *p4;
p10.printpoint();

Point p11(p1);
p11.printpoint();

Point p12(*p6);
p12.printpoint();

Point *p13 = new Point(*p7);
p13->printpoint();

delete p2;
delete p4;
delete p5;
delete p6;
delete p7;
delete p13;

return 0;
}

以及 valgrind 的输出:

==3978== Memcheck, a memory error detector
==3978== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==3978== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==3978== Command: ./Untitled2
==3978==
Point (0,0)
Point (0,0)
Point (3,4)
Point (6,7)
Point (0,0)
Point (6,7)
==3978== Conditional jump or move depends on uninitialised value(s)
==3978== at 0x400A92: Point::Point(Point const&) (Untitled2.cpp:36)
==3978== by 0x400C94: main (Untitled2.cpp:82)
==3978==
Point (3,4)
==3978== Conditional jump or move depends on uninitialised value(s)
==3978== at 0x400A92: Point::Point(Point const&) (Untitled2.cpp:36)
==3978== by 0x400CBD: main (Untitled2.cpp:85)
==3978==
==3978== Conditional jump or move depends on uninitialised value(s)
==3978== at 0x400A9E: Point::Point(Point const&) (Untitled2.cpp:38)
==3978== by 0x400CBD: main (Untitled2.cpp:85)
==3978==
==3978== Conditional jump or move depends on uninitialised value(s)
==3978== at 0x4C2C7F2: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3978== by 0x400AAE: Point::Point(Point const&) (Untitled2.cpp:38)
==3978== by 0x400CBD: main (Untitled2.cpp:85)
==3978==
==3978== Invalid free() / delete / delete[] / realloc()
==3978== at 0x4C2C83C: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3978== by 0x400AAE: Point::Point(Point const&) (Untitled2.cpp:38)
==3978== by 0x400CBD: main (Untitled2.cpp:85)
==3978== Address 0x602088 is in the Data segment of /home/kasiula/Dropbox/STH/codz/Untitled2
==3978==
Point (3,4)
==3978== Conditional jump or move depends on uninitialised value(s)
==3978== at 0x400A92: Point::Point(Point const&) (Untitled2.cpp:36)
==3978== by 0x400CE2: main (Untitled2.cpp:88)
==3978==
==3978== Conditional jump or move depends on uninitialised value(s)
==3978== at 0x400A9E: Point::Point(Point const&) (Untitled2.cpp:38)
==3978== by 0x400CE2: main (Untitled2.cpp:88)
==3978==
==3978== Conditional jump or move depends on uninitialised value(s)
==3978== at 0x4C2C7F2: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3978== by 0x400AAE: Point::Point(Point const&) (Untitled2.cpp:38)
==3978== by 0x400CE2: main (Untitled2.cpp:88)
==3978==
==3978== Invalid free() / delete / delete[] / realloc()
==3978== at 0x4C2C83C: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3978== by 0x400AAE: Point::Point(Point const&) (Untitled2.cpp:38)
==3978== by 0x400CE2: main (Untitled2.cpp:88)
==3978== Address 0x1 is not stack'd, malloc'd or (recently) free'd
==3978==
Point (0,0)
==3978== Conditional jump or move depends on uninitialised value(s)
==3978== at 0x400A92: Point::Point(Point const&) (Untitled2.cpp:36)
==3978== by 0x400D01: main (Untitled2.cpp:91)
==3978==
==3978== Conditional jump or move depends on uninitialised value(s)
==3978== at 0x400A9E: Point::Point(Point const&) (Untitled2.cpp:38)
==3978== by 0x400D01: main (Untitled2.cpp:91)
==3978==
==3978== Conditional jump or move depends on uninitialised value(s)
==3978== at 0x4C2C7F2: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3978== by 0x400AAE: Point::Point(Point const&) (Untitled2.cpp:38)
==3978== by 0x400D01: main (Untitled2.cpp:91)
==3978==
==3978== Invalid free() / delete / delete[] / realloc()
==3978== at 0x4C2C83C: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3978== by 0x400AAE: Point::Point(Point const&) (Untitled2.cpp:38)
==3978== by 0x400D01: main (Untitled2.cpp:91)
==3978== Address 0x601de8 is not stack'd, malloc'd or (recently) free'd
==3978==
Point (6,7)
==3978== Conditional jump or move depends on uninitialised value(s)
==3978== at 0x400A92: Point::Point(Point const&) (Untitled2.cpp:36)
==3978== by 0x400D23: main (Untitled2.cpp:94)
==3978==
==3978== Conditional jump or move depends on uninitialised value(s)
==3978== at 0x400A9E: Point::Point(Point const&) (Untitled2.cpp:38)
==3978== by 0x400D23: main (Untitled2.cpp:94)
==3978==
==3978== Conditional jump or move depends on uninitialised value(s)
==3978== at 0x4C2C7F2: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3978== by 0x400AAE: Point::Point(Point const&) (Untitled2.cpp:38)
==3978== by 0x400D23: main (Untitled2.cpp:94)
==3978==
==3978== Invalid free() / delete / delete[] / realloc()
==3978== at 0x4C2C83C: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3978== by 0x400AAE: Point::Point(Point const&) (Untitled2.cpp:38)
==3978== by 0x400D23: main (Untitled2.cpp:94)
==3978== Address 0x53611a8 is not stack'd, malloc'd or (recently) free'd
==3978==
Point (0,0)
==3978== Conditional jump or move depends on uninitialised value(s)
==3978== at 0x400A92: Point::Point(Point const&) (Untitled2.cpp:36)
==3978== by 0x400D42: main (Untitled2.cpp:97)
==3978==
==3978== Conditional jump or move depends on uninitialised value(s)
==3978== at 0x400A9E: Point::Point(Point const&) (Untitled2.cpp:38)
==3978== by 0x400D42: main (Untitled2.cpp:97)
==3978==
==3978== Conditional jump or move depends on uninitialised value(s)
==3978== at 0x4C2C7F2: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3978== by 0x400AAE: Point::Point(Point const&) (Untitled2.cpp:38)
==3978== by 0x400D42: main (Untitled2.cpp:97)
==3978==
==3978== Invalid free() / delete / delete[] / realloc()
==3978== at 0x4C2C83C: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3978== by 0x400AAE: Point::Point(Point const&) (Untitled2.cpp:38)
==3978== by 0x400D42: main (Untitled2.cpp:97)
==3978== Address 0xffefffb50 is on thread 1's stack
==3978== in frame #2, created by main (Untitled2.cpp:63)
==3978==
Point (6,7)
==3978== Conditional jump or move depends on uninitialised value(s)
==3978== at 0x400A92: Point::Point(Point const&) (Untitled2.cpp:36)
==3978== by 0x400D6A: main (Untitled2.cpp:100)
==3978==
Point (3,4)
==3978== Invalid free() / delete / delete[] / realloc()
==3978== at 0x4C2C2BC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3978== by 0x400DC5: main (Untitled2.cpp:105)
==3978== Address 0xffefffae0 is on thread 1's stack
==3978== in frame #1, created by main (Untitled2.cpp:63)
==3978==
==3978== Invalid read of size 8
==3978== at 0x400B06: Point::~Point() (Untitled2.cpp:49)
==3978== by 0x400DD6: main (Untitled2.cpp:106)
==3978== Address 0x5a1c180 is 0 bytes inside a block of size 8 free'd
==3978== at 0x4C2C2BC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3978== by 0x400DAC: main (Untitled2.cpp:104)
==3978==
==3978== Invalid free() / delete / delete[] / realloc()
==3978== at 0x4C2C2BC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3978== by 0x400DDE: main (Untitled2.cpp:106)
==3978== Address 0x5a1c180 is 0 bytes inside a block of size 8 free'd
==3978== at 0x4C2C2BC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3978== by 0x400DAC: main (Untitled2.cpp:104)
==3978==
==3978==
==3978== HEAP SUMMARY:
==3978== in use at exit: 0 bytes in 0 blocks
==3978== total heap usage: 15 allocs, 22 frees, 120 bytes allocated
==3978==
==3978== All heap blocks were freed -- no leaks are possible
==3978==
==3978== For counts of detected and suppressed errors, rerun with: -v
==3978== Use --track-origins=yes to see where uninitialised values come from
==3978== ERROR SUMMARY: 25 errors from 25 contexts (suppressed: 0 from 0)

最佳答案

你的复制构造函数不应该试图删除指针数组。由于您在复制构造函数中并且数组从未在成员初始化列表中初始化,您可以使用

Point::Point(const Point& p)
{
coord = new int[2];
coord[0] = p.coord[0];
coord[1] = p.coord[1];
}

作为Ed Heal指出甚至没有理由在您的类(class)中进行任何动态内存分配。您的积分等级可以简单地是

class Point
{
private :
int x;
int y;
public:
Point(int x_ = 0, int y_ = 0) : x(x_), y(y_) {}
void printpoint() { std::cout << "Point (" << x << "," << y << ")\n"; }
};

现在我们不需要复制构造函数或析构函数,因为编译器提供的将正常工作。

您还删除了从未使用 new 创建的指针。 p5 指向 p1,因此您不能对其调用 delete。然后你 delete p4 没关系,但是当你 delete p6 指向 p4 你正在执行双重 delete,这是一个很大的禁忌。

关于c++ - 深层复制构造函数 - 类 C++ 中的动态数组无效的 free()/delete/delete[]/realloc(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34685385/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com