【问题标题】:How to calculate run time memory and maximum data memory usage in following c program如何在以下 c 程序中计算运行时内存和最大数据内存使用量
【发布时间】:2023-04-03 09:29:01
【问题描述】:

这是我的代码

运行时内存将取决于操作系统,但我不想要那个级别。只想从基础级别解决这类问题。

include <studio.h>

int calculate(int n);

int number = 8;

  int main(){

  int add;

  add = calculate(number);

  return 0;

}

int calculate(int x){

  if(x==0){

    return x;

  }else{

    return x+calculate(x-1);

  }

}

【问题讨论】:

  • 使用#include &lt;stdio.h&gt; 而不是include &lt;studio.h&gt;
  • '运行时内存将取决于操作系统',没错。您的小程序 ios 如此简单,以至于代码、数据、堆栈等段将成为操作系统加载程序和虚拟内存管理器提供的初始工作集。通过检查链接器映射文件,您可以了解您的程序需要多少内存。

标签: c++ c stack memory-layout


【解决方案1】:

第一个问题,使用

#include <stdio.h> 

而不是

include <studio.h>

第二个问题,用x代替n

return x+calculate(x-1);

完整代码:

#include <stdio.h>

int calculate(int n);

int number = 8;

int main()
{
        int add;

        add = calculate(number);
        printf("%d\n", add);
        return 0;
}

int calculate(int x)
{
        int n = 0;
        if(x==0)
        {
                return x;
        }
        else
        {
                return x+calculate(x-1);
        }
}

【讨论】:

  • 这如何回答 OP 的问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-21
  • 2017-08-12
  • 2010-12-12
  • 1970-01-01
  • 2010-11-21
  • 1970-01-01
  • 2012-02-26
相关资源
最近更新 更多