简体   繁体   English

隐式int返回值C function

[英]Implicit int return value of C function

I've googled and just can't seem to find the answer to this simple question.我用谷歌搜索,似乎找不到这个简单问题的答案。

Working on a legacy code base (ported to Linux recently, and slowly updating to a new compiler) and I see a lot of在遗留代码库上工作(最近移植到 Linux,并慢慢更新到新的编译器),我看到了很多

int myfunction(...)
{
// no return...
}

I know the implicit return TYPE of a function is int, but what is the implicit return VALUE when no return is specified.我知道 function 的隐式返回 TYPE 是 int,但是当没有指定返回时隐式返回 VALUE 是什么。 I've tested and gotten 0, but that's only with gcc. Is this compiler specific or is it standard defined to 0?我已经测试并得到 0,但这仅适用于 gcc。这个编译器是特定的还是标准定义为 0?

EDIT: 12/2017 Adjusted accepted answer based upon it referencing a more recent version of the standard.编辑:12/2017 根据它引用更新版本的标准调整接受的答案。

From the '89 standard as quoted in the new testament:从新约中引用的 '89 标准:

Flowing off the end of a function is equivalent to a return with no expression.从 function 的末尾流出相当于没有表达式的返回。 In either case, the return value is undefined.在任何一种情况下,返回值都是未定义的。

That standard usually expresses the on-the-ground behavior of pre-existing implementations.该标准通常表达了预先存在的实现的实际行为。

Such a thing is possible, but only under the assumption that the return value of the function is never used.这样的事情是可能的,但前提是永远不会使用 function 的返回值。 The C11 standard says in para 6.9.1: C11 标准在第 6.9.1 段中说:

If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.如果到达终止 function 的 },并且调用者使用 function 调用的值,则行为未定义。

(AFAIR previous version of the standard had similar wording) (AFAIR 以前版本的标准有类似的措辞)

So it would be a good idea to transform all the functions of that sort that you have to void functions, so no user of such a function could be tempted to use the return value.因此,最好将所有此类函数转换为void函数,这样 function 的任何用户都不会被诱惑使用返回值。

The return statement is never mandatory at the end of a function, even if the function return type is not void . return 语句在 function 的末尾永远不是强制性的,即使 function 返回类型不是void No diagnostic is required and it is not undefined behavior.不需要诊断,也不是未定义的行为。

Example (defined behavior):示例(定义的行为):

int foo(void)
{
}

int main()
{
    foo();
}

But reading the return value of foo is undefined behavior:但是读取foo的返回值是未定义的行为:

int bla = foo();  // undefined behavior

From the C Standard:来自 C 标准:

( C99 , 6.9.1 on p.12) "If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined." C99 ,第 12 页上的 6.9.1)“如果达到终止 function 的 },并且调用者使用 function 调用的值,则行为未定义。”

The main function is an exception to this rule as if the } is reached in main it is equivalent as if there was a return 0; main function 是这个规则的一个例外,就好像}main中达到它一样,就好像有一个return 0; statement.陈述。

( C99 draft , 5.1.2.2.3 on p.13)...reaching the } that terminates the main function returns a value of 0. C99 草案,第 13 页上的 5.1.2.2.3)...到达终止main function 的}返回值 0。

That's simply undefined behaviour;那只是未定义的行为; if you don't populate the return area (which for example is generally eax/rax on x86 family processors ), it'll have the value last set up through some side-effect in your function.如果您不填充返回区域(例如,通常是x86 系列处理器上的 eax/rax ),它将具有最后通过 function 中的一些副作用设置的值。

See Is a return statement mandatory for C++ functions that do not return void?请参阅不返回 void 的 C++ 函数是否必须使用返回语句? which is basically a duplicate of this question (except that it's tagged as C++).这基本上是这个问题的副本(除了它被标记为 C++)。

If the return statements consistently do not return a value, the function is best converted to, and declared as, returning void :如果return语句始终不返回值,则最好将 function 转换为并声明为返回void

void myfunction(...)
{
    ...
    return;
    ...
}

If there are some some return expr;如果有一些return expr; and some return;还有一些return; statements in the function, then you need to decide which is the better behaviour, and make them consistent — either always return a value and leave the type as int or never return a value and change the type to void . function 中的语句,那么你需要决定哪个是更好的行为,并使它们保持一致——要么始终返回一个值并将类型保留为int ,要么从不返回值并将类型更改为void

Note that you'll need to declare functions modified to return void (in a header, unless they are — or should be — static and hidden in a single source file) since the default return type (assumed return type) of int is no longer valid.请注意,您需要声明修改为返回void的函数(在 header 中,除非它们是 - 或应该是 - static并隐藏在单个源文件中),因为int的默认返回类型(假定返回类型)不再有效的。

It may always be zero in that function, but on most architectures (certainly x86) the return statement moves the contents of a specific register to a specific place on the stack which the caller will retrieve and use as its return function.在 function 中,它可能始终为零,但在大多数体系结构(当然是 x86)上,return 语句将特定寄存器的内容移动到堆栈上的特定位置,调用者将检索该位置并将其用作其返回值 function。

The return statement will place the variable passed to it in that location so that it'll be a different value. return 语句会将传递给它的变量放在该位置,以便它成为不同的值。 My experience with not placing a specific return statement is that its fairly random what gets returned and you can't rely on it being the same thing.我没有放置特定返回语句的经验是返回的内容相当随机,您不能依赖它是同一件事。

I'm certain that it is undefined behaviour for a function whose return type is not void to omit a return statement.我确定对于返回类型不是void的 function 省略return语句是未定义的行为。 In C99 there is an exception for main , where if the return statement is omitted, it is assumed to return 0 implicitly, but this doesn't apply to any other function.在 C99 中, main有一个例外,如果省略return语句,则假定隐式返回 0,但这不适用于任何其他 function。

It may work on a specific platform/compiler combination but you should never rely on such specifics.它可能适用于特定的平台/编译器组合,但您永远不应依赖此类细节。 Using any kind of undefined behaviour in your code makes it unportable.在代码中使用任何类型的未定义行为都会使其不可移植。 It is common to see undefined behaviour in legacy code though.不过,在遗留代码中看到未定义的行为是很常见的。

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

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