2017-09-13 79 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