-6

Are the commands the computer performs the same for these two expressions in C++:

myInt += 1;
myInt ++;

Am I supposed to use a GCC? Or if you know the answer can you tell me?

  • 8
    Java doesn't compile to assembly. – shmosel Nov 06 '17 at 20:01
  • 1
    They don't have the same value: e.g. `int a = myInt += 1;` and `int a = myInt++;` result in different values of `a` (the former is like `++myInt`). So they won't compile to the same code. – Andy Turner Nov 06 '17 at 20:02
  • They will both probably be translated to the same machine-level instruction. – jrook Nov 06 '17 at 20:04
  • 3
    Why do you think you need to care about the difference between them? Write *readable* code, let the runtime worry about nano-optimization. – Andy Turner Nov 06 '17 at 20:07
  • @jrook What do you mean they will be translated to the same machine-level instruction? Java's not compiled to machine-level instructions. – EJoshuaS - Stand with Ukraine Nov 06 '17 at 20:07
  • @EJoshuaS At some point the CPU has to run an instruction to increment a variable. I am guessing that instruction will be the same for both of these idioms. – jrook Nov 06 '17 at 20:09
  • 1
    @ivomalinowskip - Incidentally, is there something preventing you from looking at the Java bytecode yourself? It's easy enough to compile something yourself to see if they result in different bytecode. – EJoshuaS - Stand with Ukraine Nov 06 '17 at 20:11
  • 1
    @jrook Yes, in most cases the bytecode will be compiled, but there's no guarantee of that - some implementations of the JVM *could* just interpret the bytecode directly, in which case there will not be a machine-level instruction. See: https://stackoverflow.com/questions/1326071/is-java-a-compiled-or-an-interpreted-programming-language - either way, the relationship between the Java code and the eventual machine code is rather indirect and implementation-specific at best. – EJoshuaS - Stand with Ukraine Nov 06 '17 at 20:13
  • FWIW, in C#/Visual Studio they're identical. – EJoshuaS - Stand with Ukraine Nov 06 '17 at 20:23
  • I changed the post to C++. – ivomalinowskip Nov 09 '17 at 15:29
  • @EJoshuaS I didn't know how to do bytecode or whatever that is and I edited the question so could you please help if you know how to? – ivomalinowskip Nov 09 '17 at 15:41
  • This may help you: https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javap.html - it shows how to view bytecode, which'll tell you whether the two lines result in the same bytecode. Keep in mind that this *may* be compiler-dependent - I'd hope that most compilers are smart enough to "know" that those actually mean the same thing (Visual Studio's C# compiler is), but that may not be the case for all of them. – EJoshuaS - Stand with Ukraine Nov 09 '17 at 15:43
  • @EJoshuaS Oh thank you very much. At first site it looks like that is what I'm looking for. Would you know something similar for C++? – ivomalinowskip Nov 09 '17 at 15:44
  • Depends on your compiler, but I think many compilers should allow you to configure them to produce both assembly language and executables (or, at least, I used to do that in Visual Studio with some regularity). – EJoshuaS - Stand with Ukraine Nov 09 '17 at 15:46
  • FWIW, in C#/Visual Studio those *do* end up compiling to the same MSIL, so I'm guessing that a lot of compilers will be "smart enough" to know that those have the same meaning. – EJoshuaS - Stand with Ukraine Nov 09 '17 at 15:47
  • @EJoshuaS Lets say I'm not very advanced so how exactly am I supposed to do what you are telling me? – ivomalinowskip Nov 09 '17 at 15:52
  • @ivomalinowskip I created an answer showing how I did that in Visual Studio - a lot of compilers will offer you the ability to do something similar. I also edited to show how to do the same thing in gcc. – EJoshuaS - Stand with Ukraine Nov 09 '17 at 15:58

1 Answers1

2

For many compilers, these will be identical. (Note that I said many compilers - see my disclaimer below). For example, for the following C++ code, both Test 1 and Test 2 will result in the same assembly language:

int main()
{
    int test = 0;

    // Test 1
    test++;

    // Test 2
    test += 1;

    return 0;
}

Many compilers (including Visual Studio) can be configured to show the resulting assembly language, which is the best way to settle questions like this. For example, in Visual Studio, I right-clicked on the project file, went to "Properties", and did the following: enter image description here

In this case, as shown below, Visual Studio does, in fact, compile them to the same assembly language:

; Line 8
    push    ebp
    mov ebp, esp
    sub esp, 204                ; 000000ccH
    push    ebx
    push    esi
    push    edi
    lea edi, DWORD PTR [ebp-204]
    mov ecx, 51                 ; 00000033H
    mov eax, -858993460             ; ccccccccH
    rep stosd
; Line 9
    mov DWORD PTR _test$[ebp], 0
; Line 12 - this is Test 1
    mov eax, DWORD PTR _test$[ebp]
    add eax, 1
    mov DWORD PTR _test$[ebp], eax
; Line 15 - This is Test 2 - note that the assembly is identical
    mov eax, DWORD PTR _test$[ebp]
    add eax, 1
    mov DWORD PTR _test$[ebp], eax
; Line 17
    xor eax, eax
; Line 18
    pop edi
    pop esi
    pop ebx
    mov esp, ebp
    pop ebp
    ret 0
_main   ENDP
_TEXT   ENDS
END

Interestingly enough, its C# compiler also produces the same MSIL (which is C#'s "equivalent" of assembly language) for similar C# code, so this apparently holds across multiple languages as well.

By the way, if you're using another compiler like gcc, you can follow the directions here to get assembly language output. According to the accepted answer, you should use the -S option, like the following:

gcc -S helloworld.c

If you're writing in Java and would like to do something similar, you can follow these directions to use javap to get the bytecode, which is Java's "equivalent" of assembly language, so to speak.

Also of interest, since this question originally asked about Java as well as C++, this question discusses the relationship between Java code, bytecode, and the eventual machine code (if any).

Caution: Different compilers may produce different assembly language, especially if you're compiling for different processors and platforms. Whether or not you have optimization turned on can also affect things. So, strictly speaking, the fact that Visual Studio is "smart enough" to know that those two statements "mean" the same thing doesn't necessarily mean that all compilers for all possible platforms will be that smart.

  • This answer was very complete and very good, thank you. And BTW do you know why I got so many dislikes? – ivomalinowskip Nov 09 '17 at 16:24
  • StackOverflow works best if the questioners do a little research themselves. So you may get downvotes because you simply didn't look _yourself_ at the assembler code. Downvotes because it's hard to understand why you are asking such a question which seems to be rather academical and not related to a real world problem. Or downvotes because you got the tags wrong at first. Or downvotes because your question is very broad: you didn't mention a processor architecture which makes quite a difference in assembler. So you see, it's not to hard to find reasons to downvote. – ventiseis Dec 04 '17 at 00:02