为什么会崩溃?我如何解决它?
Why is this crashing? How do I fix it?
这是我的代码 (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;
上崩溃。为什么会这样。 (P.S。这是另一个加密项目的一部分(可能是敏感部分 [不是崩溃点或代码错误] 变成了“----------------”(这不是实际代码)。
删除所有指针和所有 delete
。这里没有任何内容是用 new
创建的,因此没有要删除的内容。好的,应该删除 dataptr
。但这在所有取消引用的噪音中很难找到。
这是我的代码 (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;
上崩溃。为什么会这样。 (P.S。这是另一个加密项目的一部分(可能是敏感部分 [不是崩溃点或代码错误] 变成了“----------------”(这不是实际代码)。
删除所有指针和所有 delete
。这里没有任何内容是用 new
创建的,因此没有要删除的内容。好的,应该删除 dataptr
。但这在所有取消引用的噪音中很难找到。