Different Machine Codes for same piece of logic

51 views Asked by At

Consider the following code

char love[4]={'l','o','v','e'};

will the machine code of love[1] and *(love+1) be same or different if different why?

1

There are 1 answers

0
paxdiablo On BEST ANSWER

If you're asking will the reference the same memory location, the answer is yes. So will *(1+love) and 1[love].

If you're asking if the compiler will generate the same machine language under the covers, that depends entirely on the compiler. The ISO C standard does not dictate that level of detail.

It's generally more concerned with effects rather than implementation details.

Given that all four possibilities mean the same thing, I'd be surprised if a compiler generated different machine code under the covers - I'd expect a decent compiler to generate the most efficient version for all cases. However, as mentioned above, it's by no means mandatory.