【发布时间】:2016-08-06 19:51:48
【问题描述】:
这是我的代码 (C++)。
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <sstream>
using namespace std;
void rotaryxorencrypt(int dat[],int len){
------------------------------
}
void encrypt(int dat[],int len){
rotaryxorencrypt(dat,len);
}
void decrypt(int dat[],int len){
}
int main() {
fstream file;
fstream *fileptr=&file;
file.open("tmp",ios::in);
string line;
string *lineptr=&line;
int x;
int *xptr=&x;
int cont=0;
int *contptr=&cont;
int len;
int *lenptr=&len;
stringstream ss;
stringstream *ssptr=&ss;
string cryption;
string *cryptionptr=&cryption;
getline(*fileptr,*lineptr);
try{
if(*lineptr=="encryption"){
*cryptionptr="encrypt";
}else if(*lineptr=="decrypt"){
*cryptionptr="decryption";
} else {
cout<<"Unknown Cryptography Type - "<<*lineptr<<endl;
throw 0;
}
getline(*fileptr,*lineptr);
*ssptr<<*lineptr;
*ssptr>>*xptr;
ss.str("");
ss.clear();
*lenptr=*xptr;
int *dataptr;
dataptr=new int[*lenptr];
cout<<"Loading Formatted Data"<<endl;
while ( getline (*fileptr, *lineptr) ) {
*ssptr<<*lineptr;
*ssptr>>*xptr;
ss.str("");
ss.clear();
dataptr[cont]=*xptr;
cont++;
}
file.close();
delete lineptr;
delete xptr;
delete contptr;
delete ssptr;
delete fileptr;
------------------
if(*cryptionptr=="encrypt"){
cout<<"Beginning Encryption Process."<<endl;
cout<<dataptr[0]<<endl;
encrypt(dataptr,len);
cout<<dataptr[0]<<endl;
cout<<"Outputting Encrypted Data."<<endl;
}else if(*cryptionptr=="decrypt"){
cout<<"Beginning Decryption Process."<<endl;
decrypt(dataptr,len);
cout<<"Outputting Decrypted Data."<<endl;
}
cout<<"Clearing Cache."<<endl;
delete []dataptr;
delete cryptionptr;
delete lenptr;
}catch(int x){
if(x==0){
cout<<"Clearing Cache."<<endl;
delete fileptr;
delete lineptr;
delete xptr;
delete contptr;
delete ssptr;
delete fileptr;
}else{
cout<<"Unknown Error - Cannot Clear The Cache."<<endl;
}
}
cout<<"here"<<endl;
return 0;
cout<<"here"<<endl;
}
注意return 0; 前后的cout<<"here"<<endl;。没有它们我有同样的问题,所以它们不是问题,但它会执行第一个cout<<"here"<<endl;,但会在第二个之前崩溃。如果我删除第二个,它会做同样的事情,如果我删除第一个它只会崩溃,因此它会在return 0; 上崩溃。为什么会这样。 (PS 这是另一个加密项目的一部分(可能敏感部分 [不是崩溃点或代码错误] 被变成了“-----------------”(这不是实际的代码)。
【问题讨论】:
-
请发送minimal reproducible example。如果您已经转储了 100 行,那么省略代码也无济于事。你的确切输出是什么?
-
你所描述的听起来不像崩溃。您对从主例程返回结束您的程序感到惊讶吗?
-
我的确切输出是(\n 表示有一个我无法输入的输入):Loading Formatted data\nBeginning Encryption Process.\n140\n140\nOputputting Encrypted Data.\nClearing Cache。 \n这里\n(这是它崩溃的地方,但在告诉 windows (7) '关闭程序',因为它正在调试模式下运行后打印其余部分)\n进程返回 255 (0xFF) 执行时间:13.743 s\n按任意键继续。 (结束输出)。为了给你剩下的代码,我在崩溃开始之前省略的任何代码都可以工作,所以这不是问题而且它也是敏感的(这是加密程序的一部分)
-
它崩溃了,因为您在没有用 new 分配的指针上使用 delete。
-
@Justin 为什么我们要从你身上挖掘这些信息?
标签: c++ encryption crash partials