引发的异常: 0xC0000005: 读取位置 0xFFFFFFFFFFFFFFFF 时发生访问冲突.

y_yyyym 2019-04-07 11:52:06
#include <iostream>
#include <string>
#include<stdio.h>
using namespace std;

class Candidate
{
public:
Candidate();
~Candidate();
void Input(Candidate c[], int);
void Average(Candidate c[], int);
void Group(Candidate c[],Candidate WS[],Candidate MS[],Candidate LS[], int n);
void Top_k(Candidate WS[],Candidate TOPK[], int);
void Query(Candidate c[],Candidate TOPK [],Candidate WS[], Candidate MS[], Candidate LS[], int n,int k);
void Sort(Candidate TOPK[], int);
private:
string name;
float mean;
int m;
float delta; //阈值
float r=0.0;
}can[200];

Candidate::Candidate()
{
}

Candidate::~Candidate()
{
}
//输入数据和其评分
void Candidate::Input(Candidate c[], int n)
{
for (int i = 0; i < n; i++)
{
cout << "输入第" << i+1 << "个候选项的名字:";
cin >> c[i].name;
cout << "输入第" << i+1 << "个候选项评分:";
cin >> c[i].mean;
}
}
//求参考项r
void Candidate::Average(Candidate c[], int n)
{
float sum = 0.0;
for (int i = 0; i < n; i++)
{
sum += c[i].mean;
}
r = sum / n;
cout << "参考项:" << r << endl;
}

//分组
void Candidate::Group(Candidate c[], Candidate WS[], Candidate MS[], Candidate LS[], int n)
{
int a = 0; //分组后的数组索引
int b = 0;
int d = 0;

for (int i = 0; i < n; i++)
{
//进入胜者组
if ((c[i].mean > r) && (c[i].mean>r + delta))
{
WS[a].mean = c[i].mean;
WS[a].name = c[i].name;
a++;
}

else
{
//进入接近组
if ((c[i].mean >= r - delta) && (c[i].mean <= r + delta))
{
MS[b].mean = c[i].mean;
MS[b].name = c[i].name;
b++;
}
else
{
//进入败者组
if (c[i].mean < (r-delta))
{
LS[d].mean = c[i].mean;
LS[d].name = c[i].name;
//d++;
}
}
}

}
cout << "胜者组个数" << a+1 << endl;
cout << "接近组个数" << b+1 << endl;
cout<< "败者组个数" << d+1 << endl;
}

//top-k
void Candidate::Top_k(Candidate WS[],Candidate TOPK[], int k)
{
int x = 0;
//统计胜者组中数据量
for (int i = 0; i < 100000; i++)
{
if (WS[i].mean == 0)
{
x = i;
break;
}
}
cout <<"----胜者组中元素个数:"<< x << endl;
//将胜者组中的k个元素放入TOPK中,删除WS中进入TOPK的元素
for (int i = 0; i < k; i++)
{
TOPK[i].mean = WS[i].mean;
TOPK[i].name = WS[i].name;

WS[i].mean = WS[i + k].mean;
WS[i].name = WS[i + k].name;
}
//若胜者组中还有元素
if ((x-k)>0)
{
//求TOPK中最小分值
for (int i = 0; i < x-k; ++i)
{
float min = TOPK[1].mean;
int s;
for (int j = 0; j < k; j++)
{
if (TOPK[j].mean < min)
{
min = TOPK[j].mean;
s = j;
}
}
//从胜者组中找更好的
if (WS[i].mean > min)
{
TOPK[s].mean = WS[i].mean;
TOPK[s].name = WS[i].name;
}
}
}

}
//查询
void Candidate::Query(Candidate c[],Candidate TOPK[],Candidate WS[], Candidate MS[], Candidate LS[], int n,int k)
{
Candidate *R, *P;
R = new Candidate;
P = new Candidate;
while (n > 2 * k)
{
int x;
Group(can, WS, MS, LS, n);
for (int i = 0; i < n; i++)
{
if (WS[i].mean == 0)
{
x = i;
break;
}
}
if (x >= k && x <= 2 * k)
{
Top_k(WS, R, k);
}
else
{
if (x < k)
{
for (int j = 0; j < x; j++)
{
TOPK[j].mean = WS[j].mean;
TOPK[j].name = WS[j].name;
}
Top_k(MS, P, k - x);
for (int j = x; j <k; j++)
{
TOPK[j].mean = P[j - x + 1].mean;
TOPK[j].name = P[j - x + 1].name;
}

}
}
n = x;
}
}
//排序并输出
void Candidate::Sort(Candidate TOPK[], int k)
{
float Mscore;
string Mname;
for (int i = 0; i < k; i++)
{
for (int j = i + 1; j < k; j++)
{
Mscore = TOPK[i].mean;
Mname = TOPK[i].name;
if (TOPK[j].mean > TOPK[i].mean)
{
TOPK[i].mean = TOPK[j].mean;
TOPK[i].name = TOPK[j].name;
}
}
}

cout << "---最后的TOPK结果----" << endl;
for (int i = 0; i < k; i++)
cout << "名字:" << TOPK[i].name << " " << "评分:" <<TOPK[i].mean<< endl;
}
void main()
{
int k;
int N;
cout << "查询k值:" << endl;
cin >> k;
cout << "候选集项数:" << endl;
cin >> N;

Candidate c;
Candidate *WS,*MS,*LS,*TOPK;
WS = new Candidate;
MS = new Candidate;
LS = new Candidate;
TOPK = new Candidate;

c.Input(can, N);
c.Average(can, N);
c.Group(can, WS, MS, LS, N);
c.Query(can, TOPK, WS, MS, LS, N, k);
//c.Top_k(WS, TOPK, k);
c.Sort(TOPK, k);
}

调试程序出现这个异常中断后,在堆栈中发现是c.Sort(TOPK,k)这里在调用Sort时有问题,请各位大佬指点一下,是哪个指针的问题还是数组越界了
...全文
1056 点赞 收藏 2
写回复
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
pstrunner 2019-04-10
我简单调试了下,主要有如下问题 1.can被你分配固定大小的数组,但是在主程序你遍历时输入的N,如果N超过其can大小,就会越界;我修改了动态分配can,好像没有崩溃。但是后面卡死了。 2.代码命名和组织结构比较混乱,建议LZ再整理下。我简单修改的代码如下,楼主可以对照看看。

#include <iostream>
#include <string>
#include<stdio.h>
using namespace std;

class Candidate
{
public:
	Candidate() : r(0.0) {}
	~Candidate() {}
	void Input(Candidate c[], int);
	void Average(Candidate c[], int);
	void Group(Candidate c[],Candidate WS[],Candidate MS[],Candidate LS[], int n);
	void Top_k(Candidate WS[],Candidate TOPK[], int);
	void Query(Candidate c[],Candidate TOPK [],Candidate WS[], Candidate MS[], Candidate LS[], int n,int k);
	void Sort(Candidate TOPK[], int);
private:
	string name;
	float  mean;
	int m;
	float delta;  //阈值
	float r;
};

static Candidate * s_can = nullptr;

//输入数据和其评分
void Candidate::Input(Candidate c[], int n)
{
	for (int i = 0; i < n; i++)
	{
		cout << "输入第" << i+1 << "个候选项的名字:";
		cin >> c[i].name;
		cout << "输入第" << i+1 << "个候选项评分:";
		cin >> c[i].mean;
	}
}
//求参考项r
void Candidate::Average(Candidate c[], int n)
{
	float sum = 0.0;
	for (int i = 0; i < n; i++)
	{
		sum += c[i].mean;
	}
	r = sum / n;
	cout << "参考项:" << r << endl;
}

//分组
void Candidate::Group(Candidate c[], Candidate WS[], Candidate MS[], Candidate LS[], int n)
{
	int a = 0;  //分组后的数组索引
	int b = 0;
	int d = 0;

	for (int i = 0; i < n; i++)
	{
		//进入胜者组
		if ((c[i].mean > r) && (c[i].mean>r + delta))
		{
			WS[a].mean = c[i].mean;
			WS[a].name = c[i].name;
			a++;
		}

		else
		{
			//进入接近组
			if ((c[i].mean >= r - delta) && (c[i].mean <= r + delta))
			{
				MS[b].mean = c[i].mean;
				MS[b].name = c[i].name;
				b++;
			}
			else
			{
				//进入败者组
				if (c[i].mean < (r-delta))
				{
					LS[d].mean = c[i].mean;
					LS[d].name = c[i].name;
					//d++;
				}
			}
		}

	}
	cout << "胜者组个数" << a+1 << endl;
	cout << "接近组个数" << b+1 << endl;
	cout<< "败者组个数" << d+1 << endl;
}

//top-k
void Candidate::Top_k(Candidate WS[],Candidate TOPK[], int k)
{
	int x = 0;
	//统计胜者组中数据量
	for (int i = 0; i < 100000; i++)
	{
		if (WS[i].mean == 0)
		{
			x = i;
			break;
		}
	}
	cout <<"----胜者组中元素个数:"<< x << endl;
	//将胜者组中的k个元素放入TOPK中,删除WS中进入TOPK的元素
	for (int i = 0; i < k; i++)
	{
		TOPK[i].mean = WS[i].mean;
		TOPK[i].name = WS[i].name;

		WS[i].mean = WS[i + k].mean;
		WS[i].name = WS[i + k].name;
	}
	//若胜者组中还有元素
	if ((x-k)>0)
	{
		//求TOPK中最小分值
		for (int i = 0; i < x-k; ++i)
		{
			float min = TOPK[1].mean;
			int s;
			for (int j = 0; j < k; j++)
			{
				if (TOPK[j].mean < min)
				{
					min = TOPK[j].mean;
					s = j;
				}
			}
			//从胜者组中找更好的
			if (WS[i].mean > min)
			{
				TOPK[s].mean = WS[i].mean;
				TOPK[s].name = WS[i].name;
			}
		}
	}

}
//查询
void Candidate::Query(Candidate c[],Candidate TOPK[],Candidate WS[], Candidate MS[], Candidate LS[], int n,int k)
{
	Candidate *R, *P;
	R = new Candidate;
	P = new Candidate;
	while (n > 2 * k)
	{
		int x;
		Group(s_can, WS, MS, LS, n);
		for (int i = 0; i < n; i++)
		{
			if (WS[i].mean == 0)
			{
				x = i;
				break;
			}
		}
		if (x >= k && x <= 2 * k)
		{
			Top_k(WS, R, k);
		}
		else
		{
			if (x < k)
			{
				for (int j = 0; j < x; j++)
				{
					TOPK[j].mean = WS[j].mean;
					TOPK[j].name = WS[j].name;
				}
				Top_k(MS, P, k - x);
				for (int j = x; j <k; j++)
				{
					TOPK[j].mean = P[j - x + 1].mean;
					TOPK[j].name = P[j - x + 1].name;
				}

			}
		}
		n = x;
	}
}
//排序并输出
void Candidate::Sort(Candidate TOPK[], int k)
{
	float Mscore;
	string Mname;
	for (int i = 0; i < k; i++)
	{
		for (int j = i + 1; j < k; j++)
		{
			Mscore = TOPK[i].mean;
			Mname = TOPK[i].name;
			if (TOPK[j].mean > TOPK[i].mean)
			{
				TOPK[i].mean = TOPK[j].mean;
				TOPK[i].name = TOPK[j].name;
			}
		}
	}

	cout << "---最后的TOPK结果----" << endl;
	for (int i = 0; i < k; i++)
		cout << "名字:" << TOPK[i].name << "  " << "评分:" <<TOPK[i].mean<< endl;
}
void main()
{
	int k;
	int	N;
	cout << "查询k值:" << endl;
	cin >> k;
	cout << "候选集项数:" << endl;
	cin >> N;

	Candidate c;
	Candidate *WS,*MS,*LS,*TOPK;
	WS = new Candidate;
	MS = new Candidate;
	LS = new Candidate;
	TOPK = new Candidate;
	s_can = new Candidate[N];

	c.Input(s_can, N);
	c.Average(s_can, N);
	c.Group(s_can, WS, MS, LS, N);
	c.Query(s_can, TOPK, WS, MS, LS, N, k);
	//c.Top_k(WS, TOPK, k);
	c.Sort(TOPK, k);
}
回复
轻箬笠 2019-04-07
代码太长,没仔细看。不过“引发的异常: 0xC0000005: 读取位置 0xFFFFFFFFFFFFFFFF”这种错误一般就是指针操作失败。比如数组越界,或者指针指向的内存未分配,或者指针指向的内存提前释放了。
回复 2
相关推荐
发帖
其它技术问题
创建于2007-09-28

3849

社区成员

C/C++ 其它技术问题
申请成为版主
帖子事件
创建了帖子
2019-04-07 11:52
社区公告
暂无公告