[英]Does c++ allow function call outside any funtion?
I have code 我有代码
#include <stdlib.h>
void *gg = malloc(55);
int main(int argc, char **argv)
{
return 0;
}
gcc fail to compile but g++ works. gcc无法编译,但g ++有效。
So, I just want to make sure that the malloc calling happens before main is exectued. 所以,我只想确保在main被激活之前发生malloc调用。
What you do is allowed in C++. 你在C ++中允许做什么。 The C++ standard has a whole section on "Dynamic initialization of non-local variables" .
C ++标准有一节关于“非局部变量的动态初始化” 。
Looking at the assembly generated by GCC for your code is instructive: 查看GCC为您的代码生成的程序集是有益的:
Here the initializers are called through two generated functions, _GLOBAL__sub_I_gg
(which is called before main
) which in turn calls __static_initialization_and_destruction_0
. 这里初始化器通过两个生成的函数调用,
_GLOBAL__sub_I_gg
(在main
之前调用),后者又调用__static_initialization_and_destruction_0
。
It is within the body of the latter function you will find the call to malloc
. 它位于后一个函数的主体内,您将找到对
malloc
的调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.