- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想写一个简单的类来表示一个点 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/
我有一个接受以下参数的函数: int setvalue(void (*)(void *)); 为了满足参数:void (*)(void *),我创建了这样一个函数: static void *
我有以下代码: typedef void VOID; int f(void); int g(VOID); 在 C 中编译得很好(在 Fedora 10 上使用 gcc 4.3.2)。与 C++ 编译的
这个问题已经有答案了: Is f(void) deprecated in modern C and C++? [duplicate] (6 个回答) 已关闭 7 年前。 B.A.T.M.A.N./A.
我在 ASP.NET Core 3.1 项目上有以下 Identity Server 4 配置: services .AddIdentityServer(y => { y.Events.R
我们有一个 O365 租户,一切都是开箱即用的。租户放置在德国云中,而不是全局 (office.de) 中。我们还开发了一个 Office 插件,使用 OAuth 2.0 授权访问共享点。首先,我们向
我有一个如下所示的路由 routes.MapRoute( name: "Default", url: "{controller}/{action}/{i
我正在尝试使用 OAuth2.0 访问 google 文档。我已经从 Google API 控制台获取了客户端 ID 和 key 。但是当我运行这段代码时,我收到了异常。如果我遗漏了什么,有人可以建议
此代码有效: let mut b: Vec = Vec::with_capacity(a.len()); for val in a.iter() { b.push(val); } 此代码不起作
使用 client_credintials 授权类型请求 EWS oauth2 v2.0 的访问 token 时出现错误。 https://login.microsoftonline.com/tena
我通过 Java 应用程序使用 Google 电子表格时遇到了问题。我创建了应用程序,该应用程序运行了 1 年多,没有任何问题,我什至在 Create Spreadsheet using Google
如何创建 匹配所有无效 Base64 字符的正则表达式?我在堆栈上找到了 [^a-zA-Z0-9+/=\n\r].*$ 但是当我尝试时我得到了带有 - 符号的结果字符串.我根本不知道正则表达式,任何人
我从 Gitlab CI/CD Pipelines 获得错误信息:yaml invalid。问题是由 .gitlab-ci.yml 脚本的第五行引起的: - 'ssh deployer@gita
我有 3 个数据源,设置如下: @Configuration @Component public class DataSourceConfig { @Bean("foo") @Conf
你好,我想用bulkCreate ex 插入数据: [ { "typeId": 5, "devEui": "0094E796CBFCFEF9", "application_name": "Pressu
UIApplicationExitsOnSuspend 不会强制我的应用程序退出。我已经清理过目标、删除了应用程序、重建并重新安装了很多次。 我确实需要退出我的应用程序。 最佳答案 您是否链接了 SD
在 iPhone 配置门户上,显示我的 iPhone 团队配置配置文件无效。有一个“由 Xcode 管理”文本。 “续订”按钮被禁用。 我该如何解决这个问题?谢谢 最佳答案 使用 Xcode 3.2.
好的,所以今天我用我们的“实时”数据库中的新信息更新了我的数据库……从那时起,我的一个表格就出现了问题。如果您需要任何代码,请告诉我,我将对其进行编辑并发布所需的代码... 我有一个报告表格,其中有一
我有一个结构体,其中有一个元素表示为 void (*func)(); 我知道 void 指针通常用于函数指针,但我似乎无法定义该函数。我不断收到取消引用指向不完整类型的指针。我用谷歌搜索了一下但没有结
我正在尝试使用 Coldfusion 9 从 ning 网络获取凭证,所以首先这是测试 api 的 curl 语法: curl -k https://external.ningapis.com/xn/
这个问题已经有答案了: Does C have references? (2 个回答) 已关闭 4 年前。 我正在学习 C 语言引用,这是我的代码: #include int main(void)
我是一名优秀的程序员,十分优秀!