为什么会崩溃?我该如何修复它

Why is this crashing? How do I fix it?

本文关键字:何修复 崩溃 为什么      更新时间:2023-10-16

这是我的代码(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;上。为什么会发生这种情况。(附言:这是另一个加密项目的一部分(可能是敏感部分[不是崩溃点或代码错误]被变成了"------------------"(这不是实际的代码)。

去掉所有指针和所有delete s。此处没有使用new创建任何内容,因此没有任何内容可删除。好的,有dataptr应该删除。但这在所有的噪音中都很难找到。