unhandled exception in xxx.exe 0xC0000005:Access Violation

echozzj 2009-07-15 12:02:00

麻烦大家一下:
当我的程序运行到倒数第九行时(即:face_Info Info_temp(0,temp_name,xMin,xMax);face_Info是我自己定义的一个类)总跳出一个对话框说:unhandled exception in xxx.exe 0xC0000005:Access Violation,在网上查了很多资料,可是都没有找到相近的例子可以参考,故发帖。程序如下:

//////定义vector<face_Info> all_face_Info, 来存放所有面的标号, 虚实信息和xMin xMax
vector<face_Info> all_face_Info;

/////下面是从文件face.trn中读出所有面的信息, 将其存放到vector<face_Info> all_face_Info中
pFile1 = fopen("D:\\program\\face.trn", "r");
if (!pFile1)
{
cout << "cannot open the file";
}

while (!feof(pFile1))
{
face_Info Info_temp;
int temp_name=0;
double xMin=0;
double xMax=0;

char strLine[255];
fgets(strLine, 255, pFile1); //读取每一行
//判断是否头标志
if ( sscanf(strLine, "Face: face.%d", &temp_name) != 0 )
{
while(!feof(pFile1))
{
fgets(strLine, 255, pFile1);
int totaledge=0;
if (sscanf(strLine, " Total Edges: %d", &totaledge) != 0 )
{
fgets(strLine, 255, pFile1);
fgets(strLine, 255, pFile1);
totaledge=totaledge*2; //一条线有两个点
//vertex_num用来存放nface所有的点的标号, 并且同一个标号在vertex_num里出现了两次
vector<int> vertex_num(totaledge,0);
for (std::vector<int>::size_type ix=0; ix!=vertex_num.size(); ix=ix+2)
{
fgets(strLine, 255, pFile1);
if ( sscanf(strLine, " %*s No vertex.%d %*s vertex.%d %*s ", &vertex_num[ix], &vertex_num[ix+1]) == 0)
{}
else if ( sscanf(strLine, " %*s No v_vertex.%d %*s vertex.%d %*s ", &vertex_num[ix], &vertex_num[ix+1]) == 0)
{}
else if ( sscanf(strLine, " %*s No vertex.%d %*s v_vertex.%d %*s ", &vertex_num[ix], &vertex_num[ix+1]) == 0)
{}
else if ( sscanf(strLine, " %*s No v_vertex.%d %*s v_vertex.%d %*s ", &vertex_num[ix], &vertex_num[ix+1]) == 0)
{}
else{}
}

///排序
std::sort(vertex_num.begin(),vertex_num.end());
///删除重复
std::vector<int>::iterator end_unique = std::unique(vertex_num.begin(), vertex_num.end());
vertex_num.erase(end_unique, vertex_num.end());


//用个容器存放所有nface所有点的x坐标值
vector<double> xcoord_of_nface;/////最终, vector<double> xcoord_of_nface 和 vector<int> vertex_num大小相等了


for( std::vector<int>::size_type iy=0; iy!=vertex_num.size(); ++iy)
{
for( std::vector<vertex_Info>::iterator iter=all_vertex_Info.begin(); iter!=all_vertex_Info.end(); ++iter )
{
if( (*iter).name() == vertex_num[iy])
xcoord_of_nface.push_back((*iter).xcoord());
else{}
}
}

std::stable_sort(xcoord_of_nface.begin(), xcoord_of_nface.end(), isLess);


face_Info Info_temp( 1, temp_name, xcoord_of_nface.front(), xcoord_of_nface.back() );
all_face_Info.push_back(Info_temp);
break;
}
}

}
else if ( sscanf(strLine, "Face: v_face.%d", &temp_name) != 0)
{
while(!feof(pFile1))
{
fgets(strLine, 255, pFile1);
int totaledge=0;
if (sscanf(strLine, " Total Edges: %d", &totaledge) != 0 )
{
fgets(strLine, 255, pFile1);
fgets(strLine, 255, pFile1);
totaledge=totaledge*2; //一条线有两个点
//vertex_num用来存放nface所有的点的标号, 并且同一个标号在vertex_num里出现了两次
vector<int> vertex_num(totaledge,0);
for (std::vector<int>::size_type ix=0; ix!=vertex_num.size(); ix=ix+2)
{
fgets(strLine, 255, pFile1);
if ( sscanf(strLine, " %*s No vertex.%d %*s vertex.%d %*s ", &vertex_num[ix], &vertex_num[ix+1]) == 0)
{}
else if ( sscanf(strLine, " %*s No v_vertex.%d %*s vertex.%d %*s ", &vertex_num[ix], &vertex_num[ix+1]) == 0)
{}
else if ( sscanf(strLine, " %*s No vertex.%d %*s v_vertex.%d %*s ", &vertex_num[ix], &vertex_num[ix+1]) == 0)
{}
else if ( sscanf(strLine, " %*s No v_vertex.%d %*s v_vertex.%d %*s ", &vertex_num[ix], &vertex_num[ix+1]) == 0)
{}
else{}
}

///排序
std::sort(vertex_num.begin(),vertex_num.end());
///删除重复
std::vector<int>::iterator end_unique = std::unique(vertex_num.begin(), vertex_num.end());
vertex_num.erase(end_unique, vertex_num.end());


//用个容器存放所有nface所有点的x坐标值
vector<double> xcoord_of_nface;/////最终, vector<double> xcoord_of_nface 和 vector<int> vertex_num大小相等了


for( std::vector<int>::size_type iy=0; iy!=vertex_num.size(); ++iy)
{
for( std::vector<vertex_Info>::iterator iter=all_vertex_Info.begin(); iter!=all_vertex_Info.end(); ++iter )
{
if( (*iter).name() == vertex_num[iy])
xcoord_of_nface.push_back((*iter).xcoord());
else{}
}
}

std::stable_sort(xcoord_of_nface.begin(), xcoord_of_nface.end(), isLess);


face_Info Info_temp( 0, temp_name, xcoord_of_nface.front(), xcoord_of_nface.back() );
all_face_Info.push_back(Info_temp);
break;
}
}
}
else{}

}
fclose(pFile1);




这个问题让我头痛了很久

好像是class的定义出了问题,可我就是找不出问题出在哪里,唉,真是恼火

class的定义如下:


/////定义类face_Info, 用来存放face的信息
class face_Info
{
public:
face_Info(): real_or_virtual(0), name_number(0) {}
face_Info(const bool temp_real_or_virtual, const int temp_name_number): real_or_virtual(temp_real_or_virtual), name_number(temp_name_number), xcoord_Min(0.0), xcoord_Max(0.0){}
face_Info(const bool temp_real_or_virtual, const int temp_name_number, const double temp_xcoord_Min, const double temp_xcoord_Max): real_or_virtual(temp_real_or_virtual), name_number(temp_name_number), xcoord_Min(temp_xcoord_Min), xcoord_Max(temp_xcoord_Max){}

bool r_or_v() const { return real_or_virtual; }
int name() const { return name_number; }
double xMin() const { return xcoord_Min; }
double xMax() const { return xcoord_Max; }

face_Info& operator=( const face_Info & temp )
{
this->real_or_virtual=temp.real_or_virtual;
this->name_number=temp.name_number;
this->xcoord_Min=temp.xcoord_Min;
this->xcoord_Max=temp.xcoord_Max;
return *this;
}


private:
bool real_or_virtual;
int name_number;
double xcoord_Min;
double xcoord_Max;


friend ostream& operator <<(ostream& out, const face_Info & s);
};

ostream& operator <<(ostream & out,const face_Info & s)
{
out<<s.real_or_virtual<<"\t"<<( s.real_or_virtual == 1 ? "face." : "v_face." )<<setprecision(6)<<s.name_number<<"\t"<<setprecision(10)<<s.xcoord_Min<<"\t"<<setprecision(10)<<s.xcoord_Max<<"\t";
return out;
}


有一次出现了一个相同的问题出,同样是unhandled exception in xxx.exe 0xC0000005:Access Violation,但错误出现在class的定义里,在这行:

bool r_or_v() const { return real_or_virtual; }

可到底是哪里错了,我连哪里错了都不知道,让我怎么从几千行的程序里找啊
...全文
895 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
快乐鹦鹉 2009-07-15
  • 打赏
  • 举报
回复
你自己调试过吧?
face_Info Info_temp( 0, temp_name, xcoord_of_nface.front(), xcoord_of_nface.back() );
那么front(),back()是否OK?
用户 昵称 2009-07-15
  • 打赏
  • 举报
回复
多按几次F10和F11,单步执行。
oyljerry 2009-07-15
  • 打赏
  • 举报
回复
0xC0000005 -- 指针越界操作了,自己Debug一下,就可以看到你的数组,指针等可能有操作错误,先定位语句,然后查看赌赢语句各个参数变量等是否是合法指针
jingzhongrong 2009-07-15
  • 打赏
  • 举报
回复
单步调试程序,这种错误一般都是越界访问导致的,首先要确认你xcoord_of_nface是否有数据。

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧