关于C++中使用string进行异或运算?
#include <iostream>
#include <string>
#include <ctime>
using namespace std; string Key=" ";
string *p_Key=&Key;
void bitcode(string *p_src,string *p_obj)
{
for(int i=0;i<(*p_src).length();i++)
{
char c=(*p_src)[i]^Key[i];
(*p_obj)[i]=c;
}
}
void makeKey(string *p_src)
{
for(int i=0;i<(*p_src).length();i++)
(*p_Key)[i]=rand()%10+'0';
}
void main(void)
{
string srcstr="How are you?";
string *p_srcstr=&srcstr;
string objstr=" ";
string *p_objstr=&objstr;
srand(time(NULL));
makeKey(p_srcstr);
bitcode(p_srcstr,p_objstr);
cout<<"密文:"<<*p_objstr<<endl;
cout<<*p_Key<<endl;
//bitcode(p_objstr,p_srcstr); //前面运行没问题 此处运行提示内存溢出
cout<<"明文:"<<*p_srcstr<<endl;
system("pause");
}
运行后程序后部分提示string内存溢出,请问为什么呢?谢谢!