- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
#define MAX_WEIGHT 1000000
class Set
{
public:
int * parent;
int * height;
Set(int _n)
{
parent = new int[_n+1];
height = new int[_n+1];
for(int i=0; i<_n+1; i++)
{
parent[i] = i;
height[i] = 0;
}
}
~Set()
{
delete[] parent;
delete[] height;
}
int Find_Set(int _x)
{
while(parent[_x]!=_x)
{
_x = parent[_x];
}
return _x;
}
void Union_Set(int _x, int _y)
{
_x = Find_Set(_x);
_y = Find_Set(_y);
if(_x!=_y)
{
if(height[_x]>height[_y])
parent[_y] = _x;
else if(height[_x]<height[_y])
parent[_x] = _y;
else
{
parent[_y] = _x;
height[_x]++;
}
}
}
};
template<typename WEIGHT_TYPE>
class Graph
{
public:
int vNum; // num of vertices
int eNum; // num of edges
vector<pair<int, WEIGHT_TYPE>> * edges;
Graph(const char * _fileName)
{
FILE * input = fopen(_fileName, "r");
fscanf(input, "%d %d", &vNum, &eNum);
edges = new vector<pair<int, WEIGHT_TYPE>>[vNum];
for(int i=0; i<eNum; i++)
{
int idx1, idx2;
double weight;
fscanf(input, "%d %d %lf", &idx1, &idx2, &weight);
idx1--;
idx2--;
edges[idx1].push_back(make_pair(idx2, weight));
edges[idx2].push_back(make_pair(idx1, weight));
}
}
~Graph()
{
delete[] edges;
}
};
template<typename WEIGHT_TYPE>
WEIGHT_TYPE Kruskal(Graph<WEIGHT_TYPE> &_graph)
{
vector<pair<WEIGHT_TYPE, pair<int, int>>> k;
for(int i=0; i<_graph.vNum; i++)
{
for(int j=0; j<_graph.edges[i].size(); j++)
{
if(_graph.edges[i][j].second==-1)
continue;
if(i<_graph.edges[i][j].first)
k.push_back(make_pair(_graph.edges[i][j].second, make_pair(i, _graph.edges[i][j].first)));
}
}
sort(k.begin(), k.end());
WEIGHT_TYPE cost = (WEIGHT_TYPE)0;
Set s(_graph.vNum);
for(int i=0; i<k.size(); i++)
{
int idx1 = k[i].second.first;
int idx2 = k[i].second.second;
WEIGHT_TYPE weight = k[i].first;
if(s.Find_Set(idx1) != s.Find_Set(idx2))
{
s.Union_Set(idx1, idx2);
cost += weight;
}
}
return cost;
}
template<typename WEIGHT_TYPE>
WEIGHT_TYPE Second(Graph<WEIGHT_TYPE> _graph)
{
WEIGHT_TYPE cost = (WEIGHT_TYPE)INT_MAX;
WEIGHT_TYPE cost2 = (WEIGHT_TYPE)INT_MAX;
WEIGHT_TYPE result = (WEIGHT_TYPE)INT_MAX;
for(int from=0; from<_graph.eNum; from++)
{
for(int i=0; i<_graph.edges[from].size(); i++)
{
int to = _graph.edges[from][i].first;
for(int j=0; j<_graph.edges[to].size(); j++)
{
if(_graph.edges[to][j].first==from)
{
int tmp1 = _graph.edges[from][to].second;
int tmp2 = _graph.edges[to][j].second;
_graph.edges[from][to].second = -1;
_graph.edges[to][j].second = -1;
result = Kruskal(_graph);
printf("RESULT : %d\n", result);
if(result<cost)
{
cost2 = cost;
cost = result;
}
else if(result>cost && result<cost2)
{
cost2 = result;
}
_graph.edges[from][to].second = tmp1;
_graph.edges[to][j].second = tmp2;
printf("SECOND : %d\n", cost2);
}
}
}
}
return cost2;
}
int main(int argc, char **argv)
{
Graph<int> g(argv[1]);
//int cost = Kruskal<int>(g);
int cost2 = Second<int>(g);
printf("cost2: %d\n", cost2);
FILE * output = fopen(argv[2], "w");
fprintf(output, "%d", cost2);
}
好吧,我有这段代码和我的输入文件
7 12
1 2 8
1 3 5
2 3 10
2 4 2
2 5 18
3 4 3
3 6 16
4 5 12
4 6 30
4 7 14
5 7 4
6 7 26
我在网上搜索了这个错误,发现它是在你删除一个从未分配的、已经删除或从内存中释放的内存时引起的。但是我没有看到我的代码的任何部分可以释放或删除那些空内存。我也尝试使用 GDB 但由于我的知识不足,我只是发现它是由 Second 中的 Kruskal() 引起的( ) 在 main() 中....没有别的..所以我想知道我的代码中的哪个导致错误以及我如何跟踪它?
最佳答案
您没有遵守规则 5:如果您手动定义五个特殊成员函数(析构函数、复制构造函数、移动构造函数、复制赋值、移动赋值)中的任何一个,您应该显式定义所有五个,因为自动生成的那些很可能是错误的。
这里就是这种情况:
您的类有一个隐式定义的复制构造函数(它复制指针成员)。当您调用 WEIGHT_TYPE Second(Graph<WEIGHT_TYPE> _graph)
时调用此复制构造函数- 参数被复制。现在你有相同的两个拷贝 Graph
, 都带有指向同一数组的指针 vector
。第一个超出范围的(在 Second
的末尾)将 delete[]
那个指针......但最终程序结束了,Graph
在 main
将超出范围并将 delete[]
又是指针!这是双重免费。
这就是为什么您应该以遵循零规则为目标:永远不要进行显式资源管理。不要使用 new
和 delete
,使用智能指针或容器类。编译器将自动生成正确的操作(并禁止那些没有意义的操作 - 如果您的类具有指针的明确所有权,即 std::unique_ptr,则无法复制它)。
进一步阅读:https://en.cppreference.com/w/cpp/language/rule_of_three
关于c++ - 我怎么知道双重释放或损坏(出)错误是从哪里来的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53666726/
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
我目前正在尝试制作一个非常简单的应用程序,它会根据一天中的时间问候。我的代码是: open System let read() = Console.Read() let readLine() = Co
我已经运行Elasticsearch服务很长时间了,但是突然遇到了以下情况 由以下原因导致:org.elasticsearch.index.translog.TranslogCorruptedExce
我对执行以下操作的 php 重定向脚本有一个奇怪的问题: 在用户的浏览器中植入 Cookie,或者读取现有 Cookie(如果有)。 将用户重定向到另一个网址(重定向的网址是原始网址中的参数,例如 h
我正在使用 iText 7.0.0(Java 风格),似乎表格单元格 HorizontalAlignment 被忽略,因为 CENTER 和 RIGHT 都不起作用。你能重现这个吗? see th
简而言之: 我有一个可以从多个线程访问的计数器变量。尽管我已经实现了多线程读/写保护,但该变量似乎仍然以不一致的方式同时写入,导致计数器结果不正确。 深入杂草: 我使用的“for 循环”会在后台触发大
我有一个 REST 项目,在访问控制服务类中保存用户的ArrayList。一切都工作正常,直到 REST Web 服务突然抛出 java.util.NoSuchElementException。单步查
已关闭。此问题不符合Stack Overflow guidelines 。它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
当我刷新页面时,我无法显示 voteUp/Down,因为如果我执行 voteUp/Down(+1 或 -1) 并刷新页面,这会再次返回 voteUp/Down (0)。过去我使用 JSON,但社区推荐
我正在为离散时间 CPU 调度模拟器编写代码。它只是生成流程并相应地安排它们。我目前正在实现 FCFS 计划。我理解离散时间模拟器的本质,但我在用 C++ 实现时遇到了麻烦。 问题出现在handleN
尝试使用 yum 部署包时出现错误: 2016-07-07 14:14:31,296 - ERROR - error: rpmdb: BDB0113 Thread/process 6723/1
我有一个简单的同步队列 template class SynchronisedQueue { public: void Enqueue(const T& d
我正在使用 hadoop 0.20.append 和 hbase 0.90.0。我将少量数据上传到 Hbase,然后出于评估目的杀死了 HMaster 和 Namenode。在此之后,我向 Hbase
我使用 symfony 框架 1.4 创建了一个网站。我正在使用 sfguard 进行身份验证。 现在,这在 WAMP (windows) 上运行良好。我可以在不同的浏览器上登录多个帐户并使用该网站。
目前我已经实现了 HashMap private static Map cached = new HashMap(); 和 Item 是一个具有属性的对象 Date expireTime 和 byte
我试图将 2 个不同的 WPF 控件绑定(bind)到 ViewModel 中的同一属性,即 CheckBox.IsChecked 和 Expander.IsExpanded。我想要实现的行为是让 C
我希望这是一个简单的问题,但我没有找到答案。 我想让 build.gradle 文件通过替换某些变量来设置我的 Spring Boot 应用程序中的版本。这与广告一样有效: def tokens =
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
这个问题在这里已经有了答案: In a fragment shader, why can't I use a flat input integer to index a uniform array o
我已经下载了 OSM 世界地图。解析时出现异常: osm bound changeset (...) changeset Exception in thread "main" org.xml.sax.
我是一名优秀的程序员,十分优秀!