做功能占用内存空间? [英] Do functions occupy memory space?

查看:149
本文介绍了做功能占用内存空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 无效演示()
{
    的printf(演示);
}诠释的main()
{
    的printf(%P(无效*)演示);
    返回0;
}

以上code打印功能的地址演示。结果
因此,如果我们可以打印一个函数的地址,这意味着,这个功能是在存储器present并在它被占据一定的空间。结果,
因此,有多少空间,它在内存中占据着?


解决方案

  

因此​​,如果我们可以打印一个函数的地址,这意味着,这个
  功能是在存储器present并在它被占据一定的空间。


是,你写的函数被编译成一个储存在内存中code。(在跨preTED语言的情况下,code本身是保存在内存和一个跨preTER执行。)


  

所以它是有多少空间,在内存占用?


的内存量完全取决于功能。您可以写一个很长的函数或者非常短的一个。长人会需要更多的内存。用于code一般空间是不是你需要担心,不过,除非你是重症内存的限制,比如一个非常小的嵌入式系统的环境中工作的东西。在台式电脑(甚至是移动设备),拥有现代化的操作系统,虚拟内存系统会照顾移动code页面进入或退出在需要时才物理内存的,所以有很少的机会,你的$ C $按c占用太多内存。

void demo()
{
    printf("demo");
}

int main()
{
    printf("%p",(void*)demo);
    return 0;
}

The above code prints the address of function demo.
So if we can print the address of a function, that means that this function is present in the memory and is occupying some space in it.
So how much space it is occupying in the memory?

解决方案

So if we can print the address of a function, that means that this function is present in the memory and is occupying some space in it.

Yes, the functions you write are compiled into code that's stored in memory. (In the case of an interpreted language, the code itself is kept in memory and executed by an interpreter.)

So how much space it is occupying in the memory?

The amount of memory depends entirely on the function. You can write a very long function or a very short one. The long one will require more memory. Space used for code generally isn't something you need to worry about, though, unless you're working in an environment with severe memory constraints, such as on a very small embedded system. On desktop computer (or even mobile device) with a modern operating system, the virtual memory system will take care of moving pages of code into or out of physical memory as they're needed, so there's very little chance that your code will consume too much memory.

这篇关于做功能占用内存空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆