参数传递给功能之前和之后的main()被执行 [英] Parameter passing to the functions to be executed before and after main()

查看:112
本文介绍了参数传递给功能之前和之后的main()被执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

__ attribute__((小于attibute>))可用于执行功能的前后的main()构造(如设置功能)和(如清理功能)。令我惊讶的是,即使这两个编译和运行正常,如果参数也过去了。

__attribute__ ((<attibute>)) can be used to execute the functions before and after main() like constructor (as setup function) and destructor (as cleanup function). To my surprise, even these two compile and run fine, if parameters are also passed.

#include <stdio.h>

void myStartupFunction (int a, int b) __attribute__ ((constructor));
void myDestructorFunction (int a, int b) __attribute__ ((destructor));


void myStartupFunction (int a, int b)
{
    printf("Before main\n");
}

void myDestructorFunction (int a, int b)
{
    printf("After main\n");
}

int main()
{
    printf("Inside main\n");
    return 0;
}

但因为我没有控制(也许我是错的),在这两个函数,为什么提供这些设备的调用和执行。我的意思是谁将会传递参数给这些函数,如果这样的定义作出?如果OS调用这两个函数,什么参数它决定通过怎么样?

But since I have no control (maybe I am wrong) over the invocation and execution of these two functions, why such facility is provided. I mean who will pass parameters to these functions if such a definition is made? And if OS calls these two functions, what parameters it decides to pass and how?

推荐答案

这是不确定的行为C和POSIX的标准(它立足于这个C标准),AS-的。对计划执行的出发点是 5.1.2.2.1 。还有什么是超越了任何保障的标准。

This is undefined behaviour as-of the C and the POSIX-standard (which bases on the C standard for this). Starting point for program execution is main, 5.1.2.2.1. Anything else is beyond any guarantees by the standard.

__ __属性语法而不是Linux的一部分,甚至也没有涉及到它。这是一个GCC(也可能是其他人一样铛)的扩展,它提供了部分C ++ initialisers静态变量的。

The __attribute__ syntax is not part of Linux, nor even related to it. It is a gcc (and possibly others like clang) extension which partially provides C++ initialisers for static variables.

不过,GCC并不能保证你能这些扩展中使用标准库函数。实际上,它不能,因为它不控制应用程序启动code,也不是系统库。

However, gcc does not guarantee you can use standard library functions within these extensions. Actually, it cannot, because it does not control the application startup code, nor the system library.

此启动code /运行时环境(即C运行时,CRT)负责设置静态变量,初始化标准库(如内存管理的malloc &放大器;朋友)等,还称这些构造

This startup code/run-time environment (aka C run-time, "crt") is responsible to setup static variables, initialise the standard library (e.g. memory management for malloc & friends), etc. It also calls these "constructors".

所以,你如果想用这样的结构来获得运行时环境保障和库。一般情况下,还有其他的,更安全/标准来完成任何你想要的方式。例如。显式调用它们或自动生成调用。这样,你也有你传递什么样的参数,以及如何完全控制。

So, you have to get a guarantee from the run-time environment and the libraries If you want to use such constructs. In general, there are other, safer/standard ways to accomplish whatever you want. E.g. call them in main explicitly or generate the calls automatically. That way you also have full control over what arguments you pass and how.

请注意: __ __属性一般甚至没有涉及到运行功能。它仅仅是增加额外的约束/功能的函数(或类型,变量等)或告诉特殊用途的编译器。这仅仅是的构造析构函数属性这是有关你要求的行为。

Note: __attribute__ in general is not even related to "running functions". It is just to add additional constraints/features to a function (or types, variables, etc.) or tell the compiler about special usage. It is just the "constructor" and "destructor" attributes which are related to the behaviour you ask for.

借助 GCC文档没有强制要求的功能的特定的签名。 presumably这是留给运行时。您可能要检查你的目标环境。

The gcc documentation does not mandate a specific signature for the functions. Presumably this is left to the run-time. You might want to check your target environment.

这篇关于参数传递给功能之前和之后的main()被执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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