2016-04-15 61 views
-3

這是我的代碼(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; 
} 

注意cout<<"here"<<endl;之前和之後return 0;。沒有他們我有同樣的問題,所以他們不是問題,但它會執行第一個cout<<"here"<<endl;,但會在第二個之前崩潰。如果我刪除第二個,它會做同樣的事情,如果我刪除第一個,它就會崩潰,因此它崩潰在return 0;。這是爲什麼發生。 (PS這是加密的另一個項目的一部分(可能敏感部分[不是崩潰點或代碼錯誤]被轉換爲「-----------------」(這不是實際的代碼)。

+3

請做一個[mcve],如果你已經轉儲了100行,那麼它就沒有辦法省略代碼了,你的確切輸出是什麼? – Tas

+0

你所描述的聽起來不像是崩潰。感到驚訝的是,從你的主程序返回**返回結束你的程序? –

+0

我確切的outp ut是(\ n表示有一個我無法輸入的輸入):加載格式化數據\ n開始加密過程。\ n140 \ n140 \ n輸出加密數據。\ n清除緩存。\ n \ n(這是崩潰的地方)但其餘的是在告訴windows(7)在調試模式下運行時關閉程序時打印的)\ n進程返回255(0xFF)執行時間:13.743 s \ n按任意鍵繼續。 (結束輸出)。爲了給你剩下的代碼,我忽略了在崩潰之前工作的任何代碼,所以它不是問題,它也是敏感的(這是加密程序的一部分) – Justin

回答

3

擺脫所有的指針和所有的delete秒。這裏沒有什麼用new創建所以沒有什麼刪除。好了,有dataptr應該將其刪除。但是,這是非常難的所有反引用找到噪音

+0

雖然我不想刪除所有的指針,因爲使用指針是更快的,關鍵原則之一是速度是關鍵,除了'dataptr'工作,除去所有的解除引用。謝謝你太多了。 – Justin

+0

道歉是爲了,我對Python和批處理,但這是我第二天使用任何比這更難的東西 – Justin

+2

@Justin'使用指針更快'呃誰告訴你那個? – Drop

相關問題