【问题标题】:Why is this crashing? How do I fix it?为什么会崩溃?我如何解决它?
【发布时间】: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&lt;&lt;"here"&lt;&lt;endl;。没有它们我有同样的问题,所以它们不是问题,但它会执行第一个cout&lt;&lt;"here"&lt;&lt;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


【解决方案1】:

摆脱所有指针和所有deletes。这里没有任何东西是用new 创建的,所以没有什么可以删除的。好的,应该删除 dataptr。但这在所有取消引用的噪音中都很难找到。

【讨论】:

  • 虽然我不想删除所有指针,因为使用指针更快,而且关键原则之一是速度是关键,删除除 dataptr 工作之外的所有取消引用.非常感谢。
  • 道歉是为了,我对 python 和批处理有经验,但这是我第二天使用比这些更难的东西。
  • @Justin 实际上“更快且是关键原则之一”不是关键原则,下一个开发人员的可理解性是关键关键原则。至于速度,请考虑 Donald Knuth 的话:“真正的问题是程序员在错误的地方和错误的时间花费了太多时间来担心效率;过早的优化是万恶之源(或至少大多数它)在编程中。”
  • @Justin - 不是取消引用导致了问题,而是deletes 不是用new 创建的。也就是说,创建auto 对象然后立即创建指向它们的指针的代码是嘈杂且毫无意义的。使用file和使用*file_ptr没有速度差异。
猜你喜欢
  • 1970-01-01
  • 2018-08-06
  • 2015-05-03
  • 2014-05-31
  • 1970-01-01
  • 2011-08-08
  • 2016-09-26
  • 2012-04-20
  • 1970-01-01
相关资源
最近更新 更多