向量函数的c ++内存优化

时间:2017-09-13 08:56:26

标签: c++ visual-studio memory-management

我在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;
}

我的问题是,我希望在调用callPrismBat()函数之前优化我的已用内存,使其再次变为0(零)。请问有人能帮帮我吗? 谢谢!

1 个答案:

答案 0 :(得分: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;
}