为什么是一个函数调用,而不是变量的地址,用于检测堆栈增长的方向? [英] Why is a function call, rather than variable addresses, used to detect stack growth direction?

查看:191
本文介绍了为什么是一个函数调用,而不是变量的地址,用于检测堆栈增长的方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了不同的反应,以检测堆栈增长检测的问题,我的理解是,在现代建筑,堆栈可能随机增长,可能会创建了堆,等等。

I read different responses to the question of detecting stack growth detection and I understand that, in modern architectures, stack might grow randomly, might be created off heap, and so on.

然而,在这个经典面试问题,我想了解的为什么的人用一个函数调用,而不是在同一个函数比较2的局部变量。我认为必须有这样做的,但不是一个C /低水平开发[Java的:)一些特别的原因,我只是猜测。

However, in this classic interview question, I want to understand why people use a function call rather than comparing 2 local variables in the same function. I think there must be some particular reason for doing this but, not being a C/low level developer [Java :)], I am simply guessing.

下面是code我尝试:

Here is the code I tried:

void sub (int *a)  {
    int b;
    int c;
    printf ("a:%d\n", a);
    printf ("b:%d\n", &b);
    printf ("c:%d\n", &c);
    if (&b > a) {
        printf ("Stack grows up.\n");
    } else {
        printf ("Stack grows down.\n");
    }
}

int main (void) {
    int a;
    int b;
    sub (&a);
    printf ("\nHere we go again!!\n");
    if (&b > &a)  {
        printf ("Stack grows up.\n");
    } else  {
        printf ("Stack grows down.\n");
    }
    return 0;
}

我还发现这篇文章它试图优化我不明白无论是解决方案: HTTP ://www.devx.com/tips/Tip/37412

P.S:!从这个不同的响应,其他线程,好像这个问题本身是有缺陷/无关紧要,作为一个面试问题,它可能重新实施不正确的假设,除非有人研究了答案

P.S: From different responses to this and other threads, it seems like the question itself is flawed/irrelevant, as an interview question it probably re-enforces incorrect assumptions unless someone researches the answer !

谢谢!

推荐答案

的单一堆栈帧,编译器是免费的,因为它认为合适的命令局部变量,因此code

Within a single stack frame, the compiler is free to order the local variables as it sees fit, so the code:

int i;
double j;

可以有 I 之前或之后Ĵ。只要编译生成正确的code来访问变量,它可以去任何地方。

may have i before or after j. As long as the compile generates the correct code to access the variable, it can go anywhere.

事实上,除非你使用地址运算符&安培; (或以其他方式获得的地址),变量甚至可能永远不会的是< /堆栈上的EM>。它可以存储在一个寄存器中的呼叫的持续时间。

In fact, unless you use the address-of operator & (or otherwise have to get at the address), the variable may never even be on the stack. It may be stored in a register for the duration of the call.

但是,在栈帧本身放在因为受到限制,如果他们出订单的订单,函数返回将无法正常工作那么好(说得客气一点)。

However, the order in which the stack frames themselves are put are restricted since, if they're out of order, function returns won't work that well (to put it mildly).

我要提到,当然,堆栈增长的方向只在一个场景的非常的数量有限,是非常有用的。绝大多数code应该从来不计较了。如果你有兴趣在不同的体系结构,以及它们如何处理堆栈,见<一href=\"http://stackoverflow.com/questions/664744/what-is-the-direction-of-stack-growth-in-most-modern-systems/664779#664779\">this回答。

I should mention, of course, that the direction of stack growth is useful only in a very limited number of scenarios. The vast majority of code should never care about it. If you're interested in different architectures and how they handle stacks, see this answer.

这篇关于为什么是一个函数调用,而不是变量的地址,用于检测堆栈增长的方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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