2017-09-13 96 views
-3

我在C++中遇到了一些內存優化問題。我的代碼如下:向量函數的C++內存優化

void readSis(string sisName) { 
    namaFile << "Model.prism"; 
    namaFile2 << "Properties.csl"; 

    ifstream infile(sisName.c_str()); 
    Data platform; 

    // todo: split tiap = 
    platform.N = atof(readInputLine(infile).c_str()); 
    platform.Ti = atof(readInputLine(infile).c_str()); 

    // save Ti 
    ostringstream temp; 
    temp << platform.Ti; 
    Ti = temp.str(); 

    for (int i = 0; i<platform.N; i++) { 
     Component tmp; 
     tmp.componentName = readInputLine(infile); 
     tmp.X = atof(readInputLine(infile).c_str()); 
     tmp.Y = atof(readInputLine(infile).c_str()); 
     tmp.LDU = atof(readInputLine(infile).c_str()); 
     tmp.LDD = atof(readInputLine(infile).c_str()); 
     tmp.MDD = atof(readInputLine(infile).c_str()); 

     if (tmp.Y == 1) 
     tmp.MaxState = (4 * tmp.Y) - 2; 
     else if (tmp.Y>1) 
     tmp.MaxState = (4 * tmp.Y) - 3; 
     tmp.HFT = tmp.Y - tmp.X; 

     // looping to read every tmp input 
     platform.vc.push_back(tmp); 

     // make the .prism and .csl file 
     makePrism(i + 1, tmp.HFT, platform.N, tmp.componentName, tmp.X, tmp.Y, tmp.MaxState, tmp.LDD, tmp.LDU, tmp.MDD); 
     makeCSL(tmp.HFT, i + 1, platform.Ti, platform.N, tmp.X, tmp.Y, tmp.LDD, tmp.LDU); 
    } 
    OKStream << ";" << endl; 

    // export .prism and .csl into file 
    exportPrism(); 
    exportCSL(platform.Ti); 

    cout << "Model and properties have been exported!" << endl; 

    // calling prism function 
    cout << "Calling PRISM Software in C:Program Files/prism-4.3.1/bin/prism" << endl; 
    cout << "Executing model and properties......" << endl; 
    cout << "Please wait for some moments......" << endl; 

    callPrismBat(); 

    // calling readtxt function 
    cout << "Processing PFD.txt...." << endl; 
    readtxt("PFD.txt"); 

    cout << "SIL calculation has been done in file SILCalc.SIS" << endl << endl; 
} 

我的問題是,我想使它成爲0(零)再次調用callPrismBat()功能之前優化我使用的內存。任何人都可以幫助我嗎?
謝謝!

+1

請問您能詳細說明您的「某些問題」嗎? *爲什麼*你想做「內存優化」?我們無法幫助您解決一個我們並不瞭解的問題。 –

+1

你是什麼意思:「我想優化我的已用內存」? –

+1

什麼是「它」? – molbdnilo

回答

0

簡單的解決方案:將所有東西移動到callPrismBat()到一個單獨的作用域(功能或只是額外的{}),所以本地人不再存在。

void readSis(string sisName) { 
    { 
     namaFile << "Model.prism"; 
     namaFile2 << "Properties.csl"; 

     ifstream infile(sisName.c_str()); 
     Data platform; 

     // todo: split tiap = 
     platform.N = atof(readInputLine(infile).c_str()); 
     platform.Ti = atof(readInputLine(infile).c_str()); 

     // save Ti 
     ostringstream temp; 
     temp << platform.Ti; 
     Ti = temp.str(); 

     for (int i = 0; i<platform.N; i++) { 
      Component tmp; 
      tmp.componentName = readInputLine(infile); 
      tmp.X = atof(readInputLine(infile).c_str()); 
      tmp.Y = atof(readInputLine(infile).c_str()); 
      tmp.LDU = atof(readInputLine(infile).c_str()); 
      tmp.LDD = atof(readInputLine(infile).c_str()); 
      tmp.MDD = atof(readInputLine(infile).c_str()); 

      if (tmp.Y == 1) 
       tmp.MaxState = (4 * tmp.Y) - 2; 
      else if (tmp.Y>1) 
       tmp.MaxState = (4 * tmp.Y) - 3; 
      tmp.HFT = tmp.Y - tmp.X; 

      // looping to read every tmp input 
      platform.vc.push_back(tmp); 

      // make the .prism and .csl file 
      makePrism(i + 1, tmp.HFT, platform.N, tmp.componentName, tmp.X, tmp.Y, tmp.MaxState, tmp.LDD, tmp.LDU, tmp.MDD); 
      makeCSL(tmp.HFT, i + 1, platform.Ti, platform.N, tmp.X, tmp.Y, tmp.LDD, tmp.LDU); 
     } 
     OKStream << ";" << endl; 

     // export .prism and .csl into file 
     exportPrism(); 
     exportCSL(platform.Ti); 

     cout << "Model and properties have been exported!" << endl; 
    } 
    // calling prism function 
    cout << "Calling PRISM Software in C:Program Files/prism-4.3.1/bin/prism" << endl; 
    cout << "Executing model and properties......" << endl; 
    cout << "Please wait for some moments......" << endl; 

    callPrismBat(); 

    // calling readtxt function 
    cout << "Processing PFD.txt...." << endl; 
    readtxt("PFD.txt"); 

    cout << "SIL calculation has been done in file SILCalc.SIS" << endl << endl; 
} 
+0

請注意,如果編譯器可以證明析構函數中沒有任何可能影響'callPrismBat()''readtxt(「PFD.txt」)'等等的東西,那麼允許編譯器將您的代碼視爲這段代碼 – Caleth