Returns 没有 "return" 命令
Returns without a "return" command
C编程语言,用gcc编译,WSL终端bash
我写了一个递归函数,用于查找数组中的最小数字,效果很好。
/*01*/ int minimo(int array[], int n)
/*02*/ {
/*03*/ static int min = 0;
/*04*/
/*05*/ if (n == N)
/*06*/ {
/*07*/ return array[n-1];
/*08*/ }
/*09*/ else
/*10*/ {
/*11*/ min = minimo(array, n+1);
/*12*/ if(array[n]<min){
/*13*/ min = array[n];
/*14*/ }
/*15*/ }
/*16*/ }
唯一的问题是它不应该工作,因为它不会return "min"给调用者...
int main()
{
//Var
int array[N] = {10, 2, 5, 1, 7};
printf("Min: %d\n", minimo(array, 0));
}
我的担心实际上是一个问题,但不是在我的机器上,该功能可以正常工作;
这是我朋友的笔记本电脑和 IDE 上的问题,我尝试在朋友的 Macbook 上复制到 XCode,但如果未在函数末尾添加行 "return min;",它就无法工作。
在第 15-16 行之间我必须添加 return min;
/*15*/ }
return min;
/*16*/ }
我给你的问题如下:
- 一个函数如何return一个变量自动?
- 是否有可能 return 我创建的 唯一变量 (static int min)?
- 或者是与变量具有的static属性相关的"problem"?
- 跟函数的性质有关系吗(递归)?
这是我的第一个post,如果我违反了任何论坛规则,请多多包涵。
My concern is actually a problem, but not on my machine onto which the function works just fine as it is; it is a problem on my friends' laptops and IDEs, I tried copying to XCode on a friend's Macbook and it wouldn't work if the line "return min;" wasn't added at the end of the function.
未定义行为的典型示例。在一台机器上工作,但不能在另一台机器上工作。白天工作但晚上不工作。适用于一个编译器,但不适用于另一个。当您调用未定义的行为时,C 标准对代码的行为方式没有任何要求。
If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.
在您的代码中,这正是发生的情况。当您尝试打印 return 值时,您调用了未定义的行为。
与许多人认为的相反,完全允许在非 void 函数中省略 return 语句。如果您尝试 使用 不存在的 return 值,它只会成为未定义的行为。
为避免这种情况,请始终至少使用 -Wall -Wextra
.
进行编译
我想你在某处定义了 N(在你的例子中可能是 N=5)。
如果缺少 return 语句,大多数编译器会自动添加 "return 0" 语句。
* 查看评论了解更多信息。实际上这是一个未定义的行为*
在你的递归函数中,最好传递数组的剩余长度并在到达 n==0 时停止。
避免查看全局变量或定义内部函数。
int minimo(int array[], int n)
{
...
if (n == 0)
...
min = minimo(array, n-1);
...
}
(需要其他修改)
实际上你应该只在主函数上知道数组的长度,所以称它为:
printf("Min: %d\n", minimo(array, N));
How can a function return a variable automatically?
碰巧,min
变量恰好在 ABI 的正确 return 寄存器中。
Is it possible that it does return the only variable that I created (static int min)?
我认为这与它在函数退出之前被使用更相关,但我在这里猜测:这不是 C 语言定义的行为,它是偶然工作的。
Or is it a "problem" related to the static attribute that the variable has?
Does it have anything to do with the nature of the function (recursive)?
由于这是偶然行为,可能是也可能不是。随机和确定性之间的区别在于你有太多的变量以至于你放弃解释。
How can a function return a variable automatically?
它遵循协议。它知道函数的 return 应该在某个位置找到 returned 值,并且来自该函数的显式 return
将填充该位置。如果您不调用 return
,将在给定位置保留一些随机值。
Is it possible that it does return the only variable that I created (static int min)?
不,return是什么未定义。它可以是任何东西。
Or is it a "problem" related to the static attribute that the variable has?
同样,它 return 的内容未定义。
Does it have anything to do with the nature of the function (recursive)?
是也不是。如果函数被定义为外部函数,returned 值遵循另一个协议,就像静态函数一样。
在最终的代码中它可以发生任何事情,C语言不强加实现函数的方式,无论是递归的还是非递归的。例如,如果函数是递归的并且可以预先计算,则只能在调用位置替换最终值。这也是正确的,因为程序的最终结果是预期的结果,符合ISO9899
.
中定义C的操作语义。
我引用官方文档:
4 In the abstract machine, all expressions are evaluated as specified by the semantics. An
actual implementation need not evaluate part of an expression if it can deduce that its
value is not used and that no needed side effects are produced (including any caused by
calling a function or accessing a volatile object).
它也可以用调用的值替换调用,这是正确的。
所以对于你所有的问题,答案都是未定义的行为。
C编程语言,用gcc编译,WSL终端bash
我写了一个递归函数,用于查找数组中的最小数字,效果很好。
/*01*/ int minimo(int array[], int n)
/*02*/ {
/*03*/ static int min = 0;
/*04*/
/*05*/ if (n == N)
/*06*/ {
/*07*/ return array[n-1];
/*08*/ }
/*09*/ else
/*10*/ {
/*11*/ min = minimo(array, n+1);
/*12*/ if(array[n]<min){
/*13*/ min = array[n];
/*14*/ }
/*15*/ }
/*16*/ }
唯一的问题是它不应该工作,因为它不会return "min"给调用者...
int main()
{
//Var
int array[N] = {10, 2, 5, 1, 7};
printf("Min: %d\n", minimo(array, 0));
}
我的担心实际上是一个问题,但不是在我的机器上,该功能可以正常工作; 这是我朋友的笔记本电脑和 IDE 上的问题,我尝试在朋友的 Macbook 上复制到 XCode,但如果未在函数末尾添加行 "return min;",它就无法工作。
在第 15-16 行之间我必须添加 return min;
/*15*/ }
return min;
/*16*/ }
我给你的问题如下:
- 一个函数如何return一个变量自动?
- 是否有可能 return 我创建的 唯一变量 (static int min)?
- 或者是与变量具有的static属性相关的"problem"?
- 跟函数的性质有关系吗(递归)?
这是我的第一个post,如果我违反了任何论坛规则,请多多包涵。
My concern is actually a problem, but not on my machine onto which the function works just fine as it is; it is a problem on my friends' laptops and IDEs, I tried copying to XCode on a friend's Macbook and it wouldn't work if the line "return min;" wasn't added at the end of the function.
未定义行为的典型示例。在一台机器上工作,但不能在另一台机器上工作。白天工作但晚上不工作。适用于一个编译器,但不适用于另一个。当您调用未定义的行为时,C 标准对代码的行为方式没有任何要求。
If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.
在您的代码中,这正是发生的情况。当您尝试打印 return 值时,您调用了未定义的行为。
与许多人认为的相反,完全允许在非 void 函数中省略 return 语句。如果您尝试 使用 不存在的 return 值,它只会成为未定义的行为。
为避免这种情况,请始终至少使用 -Wall -Wextra
.
我想你在某处定义了 N(在你的例子中可能是 N=5)。
如果缺少 return 语句,大多数编译器会自动添加 "return 0" 语句。
* 查看评论了解更多信息。实际上这是一个未定义的行为*
在你的递归函数中,最好传递数组的剩余长度并在到达 n==0 时停止。
避免查看全局变量或定义内部函数。
int minimo(int array[], int n)
{
...
if (n == 0)
...
min = minimo(array, n-1);
...
}
(需要其他修改)
实际上你应该只在主函数上知道数组的长度,所以称它为:
printf("Min: %d\n", minimo(array, N));
How can a function return a variable automatically?
碰巧,min
变量恰好在 ABI 的正确 return 寄存器中。
Is it possible that it does return the only variable that I created (static int min)?
我认为这与它在函数退出之前被使用更相关,但我在这里猜测:这不是 C 语言定义的行为,它是偶然工作的。
Or is it a "problem" related to the static attribute that the variable has?
Does it have anything to do with the nature of the function (recursive)?
由于这是偶然行为,可能是也可能不是。随机和确定性之间的区别在于你有太多的变量以至于你放弃解释。
How can a function return a variable automatically?
它遵循协议。它知道函数的 return 应该在某个位置找到 returned 值,并且来自该函数的显式 return
将填充该位置。如果您不调用 return
,将在给定位置保留一些随机值。
Is it possible that it does return the only variable that I created (static int min)?
不,return是什么未定义。它可以是任何东西。
Or is it a "problem" related to the static attribute that the variable has?
同样,它 return 的内容未定义。
Does it have anything to do with the nature of the function (recursive)?
是也不是。如果函数被定义为外部函数,returned 值遵循另一个协议,就像静态函数一样。
在最终的代码中它可以发生任何事情,C语言不强加实现函数的方式,无论是递归的还是非递归的。例如,如果函数是递归的并且可以预先计算,则只能在调用位置替换最终值。这也是正确的,因为程序的最终结果是预期的结果,符合ISO9899
.
我引用官方文档:
4 In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced (including any caused by calling a function or accessing a volatile object).
它也可以用调用的值替换调用,这是正确的。
所以对于你所有的问题,答案都是未定义的行为。