StackOverFlow in Postfix Decrement in JAVA

huangapple go评论63阅读模式
英文:

StackOverFlow in Postfix Decrement in JAVA

问题

这个代码段中有关于递归调用和递减操作符的问题。使用前缀递减操作符时,递归调用num减1,这是为什么不会导致堆栈溢出错误的原因。而使用后缀递减操作符时,num的值传递给递归调用,然后再减1,这可能导致堆栈溢出错误。您的问题是为什么在递归调用中计算num-1不会导致错误,而计算num--会导致错误的原因。

英文:

This works fine because we are using the prefix decrement operator in the recursive call !

public static void main(String[] args) {
    doubt1(5);
}

Prefix Decrement:

static void doubt(int num)
{
    if(num==0)
        return;
    System.out.println(num);
    doubt(--num);
}

Postfix Decrement:

static void doubt1(int num)
{
    if(num==0)
        return;
    System.out.println(num);
    doubt1(num--);
}

But when we use the postfix decrement it can lead to stack over flow error , I understand why it's causing the error , but I want to know , if we use num-1 in the recursive call it doesn't give the overflow error , my question is -> why it's not giving the error coz it calculating the num-1 and passing the value to the recursive call but not calculating the num-- in the call ?

Want the explanation why it calculating the num-1 not num-- in the recursive call ? Not why it's happening !

答案1

得分: 1

doubt1(num--);

is internally implemented as

int tmp = num;
num = num - 1;
doubt1(tmp);

That is, it recursively calls the doubt1 method with the original value and therefore the condition num == 0 is never reached.

英文:
    doubt1(num--);

is internally implemented as

    int tmp = num;
    num = num - 1;
    doubt1(tmp);

That is, it recursively calls the doubt1 method with the original value and therefore the condition num == 0 is never reached.

答案2

得分: 0

当您调用一个方法时,参数会被评估,评估的结果值会被分配为方法参数的初始值。表达式num----numnum具有相同的副作用,但对于方法调用来说这是不重要的。它们评估为不同的值:前者是num减一之前的值,后者是num减一之后的值。运算符相对于操作数的顺序对此有助记作用。

因此,对于以下代码:

int n = 1;
some_method(n--);

传递给some_method()的值为1,但对于以下代码:

int n = 1;
some_method(--n);

传递给some_method()的值为0。

然而,在这两种情况下,在执行some_method()的主体之前,值0都会存储在n中。

英文:

When you invoke a method, the arguments are evaluated, and the resulting values are assigned as the initial values of the method's parameters. The expressions num-- and --num have the same side effect on num, but that is immaterial to the method invocation. The two evaluate to different values: the former to the value of num before it is decremented, and the latter to the value of num after it is decremented. The order of the operator relative to the operand is mnemonic for that.

Thus, with

int n = 1;
some_method(n--);

the value passed to some_method() is 1, but with

int n = 1;
some_method(--n);

the value passed to some_method() is 0.

In both cases, however, the value 0 is stored in n before execution of the body of some_method() commences.

答案3

得分: 0

使用一元递减操作符时,操作发生在语句之后或之前,而不是表达式之后或之前。

所以,在这种情况下,num----num 之间会有差异。

  • doubt(num--) 在调用语句之后递减,而不是在参数表达式之后。

  • double(--num) 相反,递减发生在参数表达式之前。

从编译器的角度来看,对于 doubt(--num) 语句,你有以下步骤:

  1. 获取 num
  2. 递减 num
  3. num 应用为 doubt 参数
  4. 执行带有参数值的 doubt

而对于 doubt(num--),你有以下步骤:

  1. 获取 num
  2. num 应用为 doubt 参数
  3. 递减 num(这没有影响,因为参数值已经设置)
  4. 执行带有参数值的 doubt

对于 doubt(num--) 语句,编译器在获取 num 并准备调用 doubt 之后,然后才会递减 num。与获取 num,然后递减,然后调用 doubt 不同。

英文:

First, with the unary decrement operator, the operation is occurring after, or before, the statement.
As opposed to after, or before, the expression.

So, in this situation, there will be a difference between num-- and --num.

  • doubt(num--) is decrementing after the call statement, and not after the parameter expression.

  • double(--num), the opposite is happening, the decrement is happening before the parameter expression.

Essentially, from a compiler's stand-point, for the doubt(--num) statement, you have the following.

  1. Get num
  2. Decrement num
  3. Apply num as doubt parameter
  4. Execute doubt with parameter value

Alternately, with doubt(num--) you have the following.

  1. Get num
  2. Apply num as doubt parameter
  3. Decrement num (which has no effect, since the parameter value was already set)
  4. Execute doubt with parameter value

Essentially, for the doubt(num--) statement, after the compiler has num, and is ready to call doubt, it will then decrement num.
As opposed to having num, then decrementing, and then calling doubt.

huangapple
  • 本文由 发表于 2023年5月20日 22:42:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76295775.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定