求大哥帮忙看下,未处理的“System.AccessViolationException”类型的异常出现在 set.exe 中。

zhou2214 2012-11-04 07:06:52
main.cc 源文件

#include <iostream>
#include <string>
#include <sstream>
#include <cctype>
#include <vector>
#include <cmath>
#include <bitset>
#include <algorithm>
#include <cstddef>
#include <iomanip>
#include <stdexcept>
#include <cassert>
#include "set.h"
int main(int argc,char * argv[])
{
int cnt_of_set(1);
std::cout<<"请选择要创建的集合类型,1为int型集合,2为string型集合:"<<std::endl;
int choice(0);
std::cin>>choice;
std::string obj_name;
std::cout<<"请输入要创建的集合名字(字母、数字、下划线组成,空格隔开每个名字):"<<std::endl;
while(obj_name=="")
{
std::cin.clear();
std::getline(std::cin, obj_name);
}
for(int i=0;i<obj_name.size()-1;++i)
{
if((::isalnum(obj_name[i])||obj_name[i]=='_')&&(::isspace(obj_name[i+1])))
++cnt_of_set;
}
std::string * sets_name_ptr=new std::string[cnt_of_set]();
try
{
int i(0);
std::string temp;
std::stringstream ss;
ss<<obj_name;
while(ss>>temp)
{
sets_name_ptr[i++]=temp;
}
}
catch(std::runtime_error e)
{
std::cout<<e.what()<<std::endl;
}
std::vector<set<std::string>> set_vec(cnt_of_set);

std::cout<<"请依次输入每个集合中的元素,用空格隔开每个元素,每行输入一个集合,(回车结束一行的输入):"<<std::endl;
for(int ii=0;ii<cnt_of_set;++ii)
{
std::cout<<"现在请输入集合"<<sets_name_ptr[ii]<<"中的元素,回车结束本集合的输入:"<<std::endl;
std::string input;
while(input=="")
{
std::cin.clear();
std::getline(std::cin,input);
}
bool flag_right_input(true);
do
{
int i(0);
for(std::string::size_type i(0);i<input.size();++i)
{
if( ( (::isalnum(input[i]) )|| (::isspace(input[i])) || (input[i]=='_') )==false )
{
flag_right_input=false;
i=1;
break;
}

}
if(i==0)
flag_right_input=true;
if(flag_right_input==false)
{
std::cin.clear();
std::getline(std::cin,input);
}

}
while(!flag_right_input);
size_t arr_size(1);
for(std::string::size_type i(0);i<input.size()-1;++i)
{
if(( ::isalnum(input[i])||input[i]=='_')&&::isspace(input[i+1]))
++arr_size;
}
std::string * ptr=new std::string[arr_size]();//已处理
std::stringstream ss;
std::string obj_of_type;
ss<<input;
size_t temp_add(0);
while(ss>>obj_of_type)
{
ptr[temp_add++]=obj_of_type;

}
set<std::string> obj_set(ptr,arr_size);
set_vec[ii]=obj_set;
delete []ptr;
if(ii==cnt_of_set-1)
std::cout<<"所有集合输入成功!"<<std::endl;
}
while(std::cin)
{
std::cout<<"集合并集操作符\t+\t集合交集操作符\t&\n集合补集操作符\t-"<<std::endl;
std::cout<<"\n输入操作表达式的时候,请将操作符跟集合名用空格分开。"<<std::endl;
std::cout<<"请输入操作表达式:"<<std::endl;
std::string input;
std::getline(std::cin,input);
int arr_size(1);
for(size_t i=0;i<input.size()-1;++i)
{
if( ((::isalnum(input[i]))||input[i]=='_')&&(::isspace(input[i+1])) )
++arr_size;
}
std::string * operand_name_ptr=new std::string[arr_size]();//已处理
try
{
int i(0);
std::string temp;
std::stringstream ss;
ss<<input;
while(ss>>temp)
{
operand_name_ptr[i++]=temp;
}
}
catch(std::runtime_error e)
{
std::cout<<e.what()<<std::endl;
}
int * ptr_ptr=new int[arr_size]();//已处理
std::vector<int> ivec;
for(int i=0;i<arr_size;i+=2)
{
for(int j=0;j<cnt_of_set;++j)
{
if(sets_name_ptr[j]==operand_name_ptr[i])
{
ptr_ptr[i]=1;
ivec.push_back(j);
}
}
}
bool flag_ok_operand(true);
for(int i=0;i<arr_size;i+=2)
{
if(ptr_ptr[i]==0)
{
std::cout<<"发现错误:集合 "<<operand_name_ptr[i]<<" 不存在!"<<std::endl;
flag_ok_operand=false;
}
}
bool flag_ok_operator(true);
for(int i=1;i<arr_size;i+=2)
{
if(!((operand_name_ptr[i]=="+")||(operand_name_ptr[i]=="-")||(operand_name_ptr[i]=="&")))
{
flag_ok_operator=false;
std::cout<<"发现错误:操作符 "<<operand_name_ptr[i]<<"非法!"<<std::endl;
}
}
if(flag_ok_operand&&flag_ok_operator)
{

set<std::string> transaction_set;
int _temp(1);
set<std::string> left_operand=set_vec[ivec[0]];
for(int i=1;i<arr_size;i+=2)
{

if(operand_name_ptr[i]=="+")
left_operand =left_operand+set_vec[ivec[_temp]];
if(operand_name_ptr[i]=="-")
left_operand =left_operand-set_vec[ivec[_temp]];
if(operand_name_ptr[i]=="&")
left_operand =left_operand&set_vec[ivec[_temp]];

++_temp;

}
std::cout<<"表达式产生的新集合元素为:{ "<<left_operand<<" }"<<std::endl;
delete [] operand_name_ptr;
delete [] ptr_ptr;


}

std::cin.clear();
}

delete [] sets_name_ptr;
return 0;
}


set.h 头文件


#include <iostream>
#include <string>
#include <sstream>
#include <cctype>
#include <vector>
#include <cstddef>

template<class type_name> class set
{
template <typename type_name> friend set<type_name> operator+(set<type_name>&,set<type_name>&);
template <typename type_name> friend set<type_name> operator&(set<type_name>&,set<type_name>&);
template <typename type_name> friend set<type_name> operator-(set<type_name>&,set<type_name>&);
template <typename type_name> friend std::ostream & operator<<(std::ostream &,set<type_name>&);
public:
set();
set(std::vector<type_name> &);
set(const type_name * iptr,const int size);
//virtual ~set();
private:
std::vector<type_name> ivec;

};
template <typename type_name> set<type_name>::set(){ivec=std::vector<type_name>();}
template <typename type_name> set<type_name>::set(std::vector<type_name> & vec):ivec(vec)
{}
template <typename type_name> set<type_name>::set(const type_name* iptr,const int size)
{
for(int i=0;i<size;++i)
ivec.push_back(iptr[i]);
}
template<typename type_name> set<type_name> operator+(set<type_name>& lhs,set<type_name>& rhs)
{
std::vector<type_name> vec1(lhs.ivec),vec2(rhs.ivec),vec3;
for(std::vector<type_name>::iterator iter1=vec1.begin();iter1!=vec1.end();++iter1)
vec3.push_back(*iter1);
for(std::vector<type_name>::iterator iter2=vec2.begin();iter2!=vec2.end();++iter2)
{
bool flag(true);
for(std::vector<type_name>::iterator iter1=vec1.begin();iter1!=vec1.end();++iter1)
{
if(*iter1==*iter2)
flag=false;
}
if(flag)
vec3.push_back(*iter2);
}
set<std::string> obj_set(vec3);
return obj_set;

}
template<typename type_name> set<type_name> operator-(set<type_name>& lhs,set<type_name>& rhs)
{
std::vector<type_name> vec1(lhs.ivec),vec2(rhs.ivec),vec3;
for(std::vector<type_name>::iterator iter1=vec1.begin();iter1!=vec1.end();++iter1)
{
bool flag(true);
for(std::vector<type_name>::iterator iter2=vec2.begin();iter2!=vec2.end();++iter2)
{
if(*iter1==*iter2)
flag=false;
}
if(flag)
vec3.push_back(*iter1);
}
set<std::string> obj_set(vec3);
return obj_set;
}
template<typename type_name> set<type_name> operator&(set<type_name>& lhs,set<type_name>& rhs)
{
std::vector<type_name> vec1(lhs.ivec),vec2(rhs.ivec),vec3;
for(std::vector<type_name>::iterator iter1=vec1.begin();iter1!=vec1.end();++iter1)
{
bool flag(false);
for(std::vector<type_name>::iterator iter2=vec2.begin();iter2!=vec2.end();++iter2)
{
if(*iter1==*iter2)
flag=true;
}
if(flag)
vec3.push_back(*iter1);
}
set<std::string> obj_set(vec3);
return obj_set;

}
template<typename type_name> std::ostream & operator<<(std::ostream &os,set<type_name>& st)
{
std::vector<type_name> vec(st.ivec);
for(std::vector<type_name>::iterator iter=vec.begin();iter!=vec.end();++iter)
{
os<<*iter<<"\t";
}
return os;
}



运行的时候出现异常,提示:

在 System.AccessViolationException 中第一次偶然出现的“set.exe”类型的异常
未处理的“System.AccessViolationException”类型的异常出现在 set.exe 中。

其他信息: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
...全文
530 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhou2214 2012-11-04
  • 打赏
  • 举报
回复
引用 4 楼 JiMoKuangXiangQu 的回复:
引用 3 楼 zhou2214 的回复: 那应该是调用memcpy的时候传递的dest指针有问题. 你观察你的出错汇编语句,是在向edi指向的地址写的时候出错了,那意味着这个指针有问题,也就是传递给 memcpy()的dest指针参数有问题. 自己回溯一下堆栈,看看哪个函数调用了这个memcpy().
谢谢你 ,我找到错误了。 是个逻辑错误,我动态创建的数组大小有问题。
JiMoKuangXiangQu 2012-11-04
  • 打赏
  • 举报
回复
引用 3 楼 zhou2214 的回复:
那应该是调用memcpy的时候传递的dest指针有问题. 你观察你的出错汇编语句,是在向edi指向的地址写的时候出错了,那意味着这个指针有问题,也就是传递给 memcpy()的dest指针参数有问题. 自己回溯一下堆栈,看看哪个函数调用了这个memcpy().
zhou2214 2012-11-04
  • 打赏
  • 举报
回复
JiMoKuangXiangQu 2012-11-04
  • 打赏
  • 举报
回复
MSDN: An AccessViolationException is thrown when there is an attempt to read from or write to protected memory. Associated Tips Make sure that the memory that you are attempting to access has been allocated. Automatic memory management is one of the services that the common language runtime provides. You may wish to move to managed code to take advantage of this service. For more information, see Automatic Memory Management. Make sure that the memory that you are attempting to access has not been corrupted. If several read or write operations have occurred through bad pointers, memory may be corrupted. Remarks An access violation occurs in unmanaged or unsafe code when it attempts to read or write to memory that has not been allocated, or to which it does not have access. Not all reads or writes through bad pointers lead to access violations, so an access violation usually indicates that several reads or writes have occurred through bad pointers, and that memory might be corrupted. In managed code, all references are either valid or null. Any operation that attempts to reference a null reference in verifiable code throws NullReferenceException. An access violation that occurs in unsafe managed code can be expressed as either a NullReferenceException or a AccessViolationException, depending on the platform. Access violations in unmanaged code that bubble up to managed code are always wrapped in an AccessViolationException.
JiMoKuangXiangQu 2012-11-04
  • 打赏
  • 举报
回复
太长了. 以下摘自

64,687

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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