简体   繁体   English

将内存分配给函数(定义或调用)时

[英]When memory is allocated to a function (definition or call)

When the memory is allocated to a function. 将内存分配给函数时。 For example: 例如:

int doubleMe(int smthng){ 
int dbl = 2*smthng; //line 2
return dbl; 
} 

void main(){ 
int var; 
printf("The double of var is: %d",doubleMe(var)); //line 8
}

When is memory allocated to variable dbl ? 何时将内存分配给变量dbl

  • when is defined (line 2) (compile time) 何时定义(第2行)(编译时)
  • or when function is called (line 8)(run time) 或调用函数时(第8行)(运行时)

I believe it is allocated when function is called(run-time) in stack. 我相信它是在堆栈中调用函数(运行时)时分配的。 And freed when function exits, is it? 当函数退出时释放,是吗? Would be great if someone could please explain it better. 如果有人能够更好地解释它会很棒。

This question looks similar but not quite! 这个问题看起来很相似但不完全!

The compiler generates object code of a function when it is defined. 编译器在定义函数时生成对象代码。 The generated code contains instructions to allocate memory in the stack for function local variables or it can use registers to accomodate them. 生成的代码包含在堆栈中为函数局部变量分配内存的指令,或者它可以使用寄存器来容纳它们。

Where a function is called the compiler generates object code of the function call and corresponding instructions to push arguments on the stack. 在调用函数的地方,编译器生成函数调用的目标代码和相应的指令以推送堆栈上的参数。 At this point the compiler may not to know how the function is defined and whether it is defined because its definition can be in some other module or library. 此时,编译器可能不知道如何定义函数以及是否定义了函数,因为它的定义可以在某个其他模块或库中。

Take into account that the compiler may inline functions even if you yourself do not use function specifier inline . 考虑到编译器可能内联函数,即使您自己不使用函数说明符inline In this case it will place the function definition in the point where the function is called. 在这种情况下,它会将函数定义放在调用函数的位置。

Regarding your statement: Memory is allocated to variable dbl(or var) at compile time 关于你的陈述: 在编译时将内存分配给变量dbl(或var)
No, The instructions to allocate memory are created at compile time. 不,分配内存的指令是在编译时创建的。
Memory is not allocated until that function is executed. 在执行该功能之前,不会分配内存
Memory created inside a function is an example of local scope memory. 在函数内创建的内存是本地范围内存的示例。 if it is created on the stack, it will be released upon leaving the scope in which it was created. 如果它是在堆栈上创建的,它将在离开创建它的范围时被释放。 If it was created on the heap (ie created using [m/c/re]alloc() functions) it will not be released (or more accurately, made available as described here ) until free() is called, or at program exit. 如果它是在堆上创建(即,使用创建的[m/c/re]alloc()它不会被释放功能)(或更精确地,如所描述的提供 在这里 ),直到free()被调用时,或在程序退出。

Later, you state: 之后,你说:
I believe it is allocated when function is called. 我相信它是在调用函数时分配的。 And freed when function exits. 当函数退出时释放。
This statement is true , but this all happens at run-time only. 这句话是正确的 ,但这一切都只在运行时发生。 Not at compile-time. 不是在编译时。

For global scope memory, stack memory is created when the program is executed, and is not released until the program ends. 对于全局范围内存,在执行程序时会创建堆栈内存,并且在程序结束之前不会释放堆栈内存。 Again, for heap memory , it will be released upon calling free() , or at program exit. 同样,对于内存,它将在调用free()或程序退出时释放。

Memory is allocated to variable dbl(or var) at compile time in the stack memory 内存在编译时在堆栈内存中分配给变量dbl(或var)

Not correct, it is allocated in run-time, just as all other stack variables. 不正确,它在运行时分配,就像所有其他堆栈变量一样。 That's the whole point of using a stack. 这就是使用堆栈的重点。 It is allocated when your function is called, in run-time. 在运行时调用函数时会分配它。

I believe it is allocated when function is called. 我相信它是在调用函数时分配的。 And freed when function exits, is it? 当函数退出时释放,是吗?

Yes. 是。

It's not allocated during compilation at all, but at runtime. 它根本不是在编译期间分配的,而是在运行时分配的。

When the function is called during execution, either space will be reserved for it in memory (in approximately 100% of current C++ implementations, "on the stack") or it will reside in a register. 在执行期间调用该函数时,将在内存中为其保留任一空间(在大约100%的当前C ++实现中,“在堆栈上”)或者它将驻留在寄存器中。
If it's not in a register, the space is freed when the function returns. 如果它不在寄存器中,则在函数返回时释放空间。

The compiler produces code that will perform the runtime allocation, if there is any. 编译器生成将执行运行时分配的代码(如果有)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM