为什么执行在c ++中的return语句后停止? [英] Why execution stops after return statement in c++?

查看:280
本文介绍了为什么执行在c ++中的return语句后停止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说,语句在return语句之前执行。
在8085汇编器中, RET 语句在主程序中的函数调用之后将程序计数器返回到下一条指令。



为什么会发生这种情况?



这是一个缺点吗?

如果我想改变实例变量的值, div class =h2_lin>解决方案

语义中的 return 语句是将控制权返回给调用者(如果给定)一些返回值(被调用函数的结果)。您可以参阅调用堆栈续续以获得更广泛的图片。



n1570 C11 标准指定:


终止当前函数的执行并将控制权返回给
它的调用者。一个函数可以有任意数量的返回语句。



如果执行一个带有表达式的return语句,表达式的值是
返回给调用者函数调用表达式的值。如果表达式有一个
类型不同于其出现的函数的返回类型,则该值是
,通过赋值给具有该函数的返回类型的对象来转换。


在C ++中,事情是不同的,因为析构函数(所有构造的局部值在范围内)被调用。但是返回后没有明确的语句执行。



n3797 C ++ 14 标准在§6.6.3中明确指出:


一个函数通过返回语句返回到它的调用者。



没有表达式和braced-init-list的返回语句只能在没有
返回一个值,即返回类型为cv void,构造函数(12.1)或析构函数(12.4)的函数。
具有非void类型表达式的return语句只能在返回值的函数中使用;表达式的
值返回给函数的调用者。表达式的值隐式地
转换为它出现的函数的返回类型。返回语句可以涉及
构造和临时对象的复制或移动(§12.2)。


... .etc ....(析构函数在别处解释)






C和C ++是不同的语言。您需要阅读相关图书或网站的C编程和/或 C ++编程



不要在论坛中教授 return 的所有属性。阅读书籍,然后使用代码示例询问一些精确问题。不要忘记在计算机上测试您的代码,例如通过编译它与所有警告和调试信息(例如对于C代码 gcc -Wall -Wextra -g g ++ -Wall -Wextra -g 用于C ++代码,如果使用 GCC ...编译)。然后使用调试器(例如 gdb )逐步运行您的程序。害怕未定义的行为



优化编译器不会总是编译返回源语句转换为 RET 机器指令。它可以内联函数调用,或将 return -s编译为跳转到 function epilogue



ABI 调用约定指定如何返回值。在 Linux / x86-64 上,它通常通过%rax 注册(通常,当你返回两个标量的 struct 时,它通过两个寄存器返回)。



您可以要求您的编译器(例如使用 g ++ -O -Wall -fverbose-asm -S )生成汇编代码,它与编辑器或传呼机。



C是一种困难的语言,而C ++更困难。如果您正在学习编程,您可以使用计划 sicp /rel =nofollow> SICP 。它只有表达式,不需要任何 return 语句(因为它没有语句!)从函数返回值。 / p>

It's ok to say that the statements are executed before the return statements. In 8085 assembler, the RET statement returns the program counter to the next instruction after the function call in the main program.

Why does this happen? What if I would like to change the values of the instance variables after returning the value to the function.

Is this a drawback?

解决方案

The semantics of the  return statement in C is to give control back to the caller with (if given) some returned value (result of the called function). You might read about call stacks and continuations to get a broader picture.

The §6.8.6.4 of draft n1570 of the C11 standard specifies:

A return statement terminates execution of the current function and returns control to its caller. A function may have any number of return statements.

If a return statement with an expression is executed, the value of the expression is returned to the caller as the value of the function call expression. If the expression has a type different from the return type of the function in which it appears, the value is converted as if by assignment to an object having the return type of the function.

In C++ things are different, since destructors (of all constructed local values in scope) are called. But no explicit statements after the return gets executed.

The n3797 draft of C++14 standard says in §6.6.3 notably:

A function returns to its caller by the return statement.

A return statement with neither an expression nor a braced-init-list can be used only in functions that do not return a value, that is, a function with the return type cv void, a constructor (12.1), or a destructor (12.4). A return statement with an expression of non-void type can be used only in functions returning a value; the value of the expression is returned to the caller of the function. The value of the expression is implicitly converted to the return type of the function in which it appears. A return statement can involve the construction and copy or move of a temporary object (§12.2).

....etc.... (the destructor thing is explained elsewhere)


C and C++ are different languages. You need to read relevant books or websites on C programming and/or on C++ programming.

Don't expect to be taught all the subtilities of return in a forum. Read books, then ask some precise questions with a code sample. Don't forget to test your code on your computer, e.g. by compiling it with all warnings and debug info (e.g. gcc -Wall -Wextra -g for C code, and g++ -Wall -Wextra -g for C++ code, if compiling with  GCC...). Then use the debugger (e.g. gdb) to run your program step by step. Be afraid of undefined behavior.

An optimizing compiler won't always compile a return source statement into a RET machine instruction. It could inline the function call, or compile the return-s as jumps to a function epilogue.

The ABI and calling conventions dictate how a value gets returned. On Linux/x86-64 it is often passed thru the %rax register (often, when your return a struct of two scalars, it gets returned thru two registers).

You could ask your compiler (e.g. with g++ -O -Wall -fverbose-asm -S) to produce assembler code, and look into it with an editor or pager.

C is a difficult language, and C++ is even more difficult. If you are learning programming, you might learn Scheme with SICP. It has only expressions, and don't need any return statement (because it has no statement!) to return a value from a function.

这篇关于为什么执行在c ++中的return语句后停止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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