编译时间和运行时间 [英] compile time and run time

查看:204
本文介绍了编译时间和运行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

有人请解释一下编译时间和运行时间是什么?他们说静态变量的内存将在编译时分配?是否意味着它是在创建.exe时分配的文件?请解释我是不是错了。我是初学者所以请你清楚解释一下。

Hello,
Will someone please explain me what is compile time and run time?They say memory for static variable will be allocated at compile time?Does it mean it is allocated during creation of .exe file?Please explain if i were wrong.I am a beginner so please explain me clearly.

推荐答案

执行分配的代码由编译器生成。在程序运行之前不能进行实际分配。
The code to perform the allocation is generated by the compiler. The actual allocation cannot occur until the program is running.


@weaknessforcats


你好,

你假设只生成用于分配内存的代码。这仅适用于静态变量吗?因为函数的自动(或本地)或局部变量只应在调用函数时分配内存,即在程序执行期间。将是运行时分配?还是为了那个代码也会在编译时生成?请清楚地解释一下编译时间和运行时间是什么?


感谢您的回复
@weaknessforcats
Hello,
You say that only code will be generated for allocation of memory.Is this true only for static variables?Because auto(or local) or local variables of a function should be allocated memory only when the function is called i.e during program execution.Will that be run time allocation?Or for that too code will be generatedduring compile time?Please clearly explain me what is compile time and run time?

Thanks for your reply


Lety我问你一个问题:

如果你在机器A上编译并在机器B上运行程序,那么如何在编译时在机器A上分配内存?


显然,所有内存都是在运行时分配的。


编译器只生成分配代码而不进行实际分配。


这并不难:

1)变量outode函数在main()启动之前的运行时分配。

2)函数内部的变量在调用函数时分配。在C ++中,将在调用函数之后但在它完成之前分配变量。

函数内部的静态变量不与其他变量保持,因为它不属于特定的功能调用。相反,这个静态变量保存在内存区域,当函数完成时不会被释放。


编译器生成代码以在第一次调用时创建静态变量并且在所有opther调用中,生成使用在第一次调用时创建的相同变量的代码。


请记住,编译时没有运行该程序。
Lety me ask you a question:

If you compile on machine A and run the program on machine B, then how can memory be allocated at compile time on machine A?

Obviously, all memory is allocated at run time.

The compiler only generates the code to do the allocating but does not do the actual allocating.

It''s not that hard:
1) variables outsode functions are allocated at run time before main() starts.
2) variables inside functions are allocated when the function is called. In C++ there will be variables allocated after the function is called but before it completes.

A static variable inside a function is not kept with the other variables since it does not belong to a specific function call. Instead, this static variable is kept in an area of memory that will not be deallocated when the function completes.

The compiler generates code to create an initialize the static variable on the first call and on all opther calls, generates code that uses the same variable created on the first call.

Remember, you are not running the program when you compile it.


这篇关于编译时间和运行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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