C 编程:从汇编程序的角度来看 ++i 和 i=i+1 之间的区别?
这是一个面试问题。 我说它们是相同的,但这被认为是错误的回答。 从汇编程序的角度来看,有什么可以想象的差异吗? 我使用默认的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
面试官可能想要这样的答案:
FWIW,事实上,您知道如何看待汇编,这使您成为比我多年来采访过的 90% 的人更好的程序员。 请放心,您不必与面试您的无知失败者一起工作。
The interviewer may have wanted an answer something like this:
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.
看来你是对的,他们是错的。 我在面试时也遇到过类似的问题,我给出了正确的答案,但被认为是不正确的。
我自信地与面试官争论了这一点,他显然对我的无礼感到生气。 我没有得到这份工作,但话又说回来,在一个“无所不知”的人手下工作也不是那么理想。
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.
你可能是对的。 一个天真的编译器可能会这样做:
但是
任何半明智的编译器都会优化将 1 添加到第一个版本。
这一切都假设相关架构具有 inc 和 add 指令(就像 x86 那样)。
You are probably right. A naive compiler might do:
and
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).
为了捍卫面试官,背景就是一切。 i 是什么类型? 我们正在谈论 C 或 C++(或其他类似 C 的语言)吗? 是否给了你:
或者有更多背景信息?
如果有人问我这个问题,我的第一反应会是“我不稳定吗?” 如果答案是肯定的,那么差异就很大了。 如果不是,那么差异在语义上就很小,但在实用上却没有。 证明是解析树的差异,以及生成的子树的最终含义。
所以听起来你的务实方面是对的,但语义/批判性思维方面是错误的。
为了攻击面试官(没有上下文),我必须想知道这个问题的目的是什么。 如果我问这个问题,我想用它来了解候选人是否知道细微的语义差异,如何生成解析树,如何批判性思考等等。 我通常会向面试者提出一个 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:
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.
在 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.上下文是这里的主要内容,因为在优化的版本构建中,如果 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.