C 编程:从汇编程序的角度来看 ++i 和 i=i+1 之间的区别?

发布于 2024-07-19 05:41:01 字数 113 浏览 11 评论 0原文

这是一个面试问题。 我说它们是相同的,但这被认为是错误的回答。 从汇编程序的角度来看,有什么可以想象的差异吗? 我使用默认的 gcc 优化和 -S 编译了两个简短的 C 程序来查看汇编器输出,它们是相同的。

This was an interview question. I said they were the same, but this was adjudged an incorrect response. From the assembler point of view, is there any imaginable difference? I have compiled two short C programs using default gcc optimization and -S to see the assembler output, and they are the same.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

国际总奸 2024-07-26 05:41:01

面试官可能想要这样的答案:

i=i+1 必须加载 i 的值,加一,然后将结果存储回 i >。 相反,++i 可以简单地使用单个汇编指令来增加值,因此理论上它可能更有效。 然而,大多数编译器都会优化掉差异,生成的代码将完全相同。

FWIW,事实上,您知道如何看待汇编,这使您成为比我多年来采访过的 90% 的人更好的程序员。 请放心,您不必与面试您的无知失败者一起工作。

The interviewer may have wanted an answer something like this:

i=i+1 will have to load the value of i, add one to it, and then store the result back to i. In contrast, ++i may simply increment the value using a single assembly instruction, so in theory it could be more efficient. However, most compilers will optimize away the difference, and the generated code will be exactly the same.

FWIW, the fact that you know how to look at assembly makes you a better programmer than 90% of the people I've had to interview over the years. Take solace in the fact that you won't have to work with the clueless loser who interviewed you.

烟若柳尘 2024-07-26 05:41:01

看来你是对的,他们是错的。 我在面试时也遇到过类似的问题,我给出了正确的答案,但被认为是不正确的。

我自信地与面试官争论了这一点,他显然对我的无礼感到生气。 我没有得到这份工作,但话又说回来,在一个“无所不知”的人手下工作也不是那么理想。

Looks like you were right and they were wrong. I had a similar issue in a job interview, where I gave the correct answer that was deemed incorrect.

I confidently argued the point with my interviewer, who obviously took offence to my impudence. I didn't get the job, but then again, working under someone who "knows everything" wouldn't be that desirable either.

允世 2024-07-26 05:41:01

你可能是对的。 一个天真的编译器可能会这样做:

++i to inc [ax]

但是

i = i + 1 to add [ax], 1

任何半明智的编译器都会优化将 1 添加到第一个版本。

这一切都假设相关架构具有 inc 和 add 指令(就像 x86 那样)。

You are probably right. A naive compiler might do:

++i to inc [ax]

and

i = i + 1 to add [ax], 1

but any half sensible compiler will just optimize adding 1 to the first version.

This all assumes the relevant architecture has inc and add instructions (like x86 does).

浅沫记忆 2024-07-26 05:41:01

为了捍卫面试官,背景就是一切。 i 是什么类型? 我们正在谈论 C 或 C++(或其他类似 C 的语言)吗? 是否给了你:

++i;
i = i + 1;

或者有更多背景信息?

如果有人问我这个问题,我的第一反应会是“我不稳定吗?” 如果答案是肯定的,那么差异就很大了。 如果不是,那么差异在语义上就很小,但在实用上却没有。 证明是解析树的差异,以及生成的子树的最终含义。

所以听起来你的务实方面是对的,但语义/批判性思维方面是错误的。

为了攻击面试官(没有上下文),我必须想知道这个问题的目的是什么。 如果我问这个问题,我想用它来了解候选人是否知道细微的语义差异,如何生成解析树,如何批判性思考等等。 我通常会向面试者提出一个 C 问题,几乎每个候选人都会犯错——这是设计使然。 我实际上并不关心问题的答案,我关心的是旅程我将与候选人一起以达成理解,这告诉我的不仅仅是一个琐事问题上的对/错。

To defend the interviewer, context is everything. What is the type of i? Are we talking C or C++ (or some other C like language)? Were you given:

++i;
i = i + 1;

or was there more context?

If I had been asked this, my first response would've been "is i volatile?" If the answer is yes, then the difference is huge. If not, the difference is small and semantic, but pragmatically none. The proof of that is the difference in parse tree, and the ultimate meaning of the subtrees generated.

So it sounds like you got the pragmatic side right, but the semantic/critical thought side wrong.

To attack the interviewer (without context), I'd have to wonder what the purpose of the question was. If I asked the question, I'd want to use it to find out if the candidate knew subtle semantic differences, how to generate a parse tree, how to think critically and so on and so forth. I typically ask a C question of my interviewees that nearly every candidate gets wrong - and that's by design. I actually don't care about the answer to the question, I care about the journey that I will be taking with the candidate to reach understanding, which tells me far more about than right/wrong on a trivia question.

小糖芽 2024-07-26 05:41:01

在 C++ 中,这取决于 i 是 int 还是对象。 如果它是一个对象,它可能会生成一个临时实例。

In C++, it depends if i is an int or an object. If it's an object, it would probably generate a temporary instance.

有木有妳兜一样 2024-07-26 05:41:01

上下文是这里的主要内容,因为在优化的版本构建中,如果 i++ 可用于简单的 [inc eax],编译器将优化它。 而像 int some_int = i++ 这样的东西需要首先将 i 值存储在 some_int 中,然后才递增 i。

the context is the main thing here because on a optimized release build the compiler will optimize away the i++ if its available to a simple [inc eax]. whereas something like int some_int = i++ would need to store the i value in some_int FIRST and only then increment i.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文