【问题标题】:c++ memory optimization for vector function向量函数的c ++内存优化
【发布时间】:2018-02-21 21:40:33
【问题描述】:

我在 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(零)。请问谁能帮帮我?
谢谢!

【问题讨论】:

  • 您能否详细说明您遇到的“一些问题”? 为什么要做“内存优化”?我们无法帮助您解决我们并不真正了解的问题。
  • “我想优化我使用的内存”是什么意思?
  • “它”是什么?
  • 我的意思是,我想在运行 callPrismBat() 函数之前释放用于运行这些函数的已用内存:exportPrism() 和 exportCSL(platform.Ti)。它就像 free() 函数,但我仍然不明白如何在我的代码中实现它。

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


【解决方案1】:

简单的解决方案:将直到 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;
}

【讨论】:

  • 请注意,如果编译器可以证明析构函数中没有发生任何可能影响callPrismBat()readtxt("PFD.txt")等的事情,则允许编译器将您的代码视为此代码。
猜你喜欢
  • 2012-10-15
  • 2020-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多