C Programming: difference between ++i and i=i+1 from an assembler point of view?

后端 未结 6 509
予麋鹿
予麋鹿 2021-02-05 11:53

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 h

相关标签:
6条回答
  • 2021-02-05 12:15

    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).

    0 讨论(0)
  • 2021-02-05 12:17

    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.

    0 讨论(0)
  • 2021-02-05 12:28

    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.

    0 讨论(0)
  • 2021-02-05 12:33

    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.

    0 讨论(0)
  • 2021-02-05 12:39

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

    0 讨论(0)
  • 2021-02-05 12:40

    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.

    0 讨论(0)
提交回复
热议问题