Small traps of A Compiler

Source: Internet
Author: User

Small traps of A Compiler

Sometimes a platform runs correctlyProgramIn another platform. During platform migration, the most common problems may be the issue of byte order and alignment. This article records a small trap I have encountered before. Take a look at this sectionCodeWhat is the output?

# Include "stdio. H "char do_something (int * P) {* P = 5; return 'X';} void test1 (void) {char s [] =" abcdef "; int Index = 0; s [Index] = do_something (& Index); printf ("% s/n", S) ;}int main (void) {test1 (); return 0 ;}

If we use the VC or arm compiler, the output is abcdex. If GCC is used, the output is xbcdef. The key lies in this sentence:

S [Index] = do_something (& Index );

The index on the left of the equal sign is changed on the right of the equal sign. The VC and arm compilers use the changed index (value: 5), and the GCC compiler uses the changed index (value: 0 ). Of course, the code I actually encountered is much more complicated. Finding this line of code in tens of thousands of lines of code is not a pleasant thing. In fact, the best way to solve such a compiler trap is: Do not write statements that may produce ambiguity. In this example, we should remember:

  • In the value assignment statement, the array index or pointer offset used on the left side of the equal sign should not be changed on the right side of the equal sign.

Have you ever encountered a similar trap?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.