A series of interesting problems caused by operational order

Source: Internet
Author: User

Problem: recursion is used to calculate the sum of elements in a given integer array. The following describes the implementation of several recursive algorithms: copy the Code 1 int sum1 (int a [], int n) 2 {3 if (n> 0) 4 return a [n-1] + sum1 (a, n-1); 5 else 6 return 0; 7} 8 9 int sum2 (int a [], int n) 10 {11 if (n> 0) 12 return a [n-1] + sum2 (a, -- n); 13 else14 return 0; 15} 16 17 int sum3 (int a [], int n) 18 {19 if (n> 0) 20 return a [-- n] + sum3 (a, n-1 ); 21 else22 return 0; 23} 24 25 int sum4 (int a [], int n) 26 {27 if (n> 0) 28 return a [n] + sum4 (, -- n); 29 els E30 return 0; 31} 32 33 int sum5 (int a [], int n) 34 {35 if (n> 0) 36 return a [-- n] + sum5 (, n); 37 else38 return 0; 39} 40 41 int sum6 (int a [], int n) 42 {43 while (n> 0) 44 return a [n-1] + sum6 (a, n-1); 45 46 return 0; 47} copy the code to verify and compare the implementation of the above algorithms. Write the test code as follows: copy code 1 int main (void) 2 {3 int a [] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 4 printf ("sum1 = % d \ n", sum1 (a, 10); 5 printf ("sum2 = % d \ n", sum2 (a, 10 )); 6 print F ("sum3 = % d \ n", sum3 (a, 10); 7 printf ("sum4 = % d \ n", sum4 (a, 10 )); 8 printf ("sum5 = % d \ n", sum5 (a, 10); 9 printf ("sum6 = % d \ n", sum6 (a, 10 )); 10 return 0; 11} The execution result of the copied code in the GCC3.2.3 compiler is: 1 sum1 = 552 sum2 = 553 sum3 = 304 sum4 =-1076625666 (random value) 5 sum5 = 556 sum6 = 55 and the execution result in the VC6.0 compiler is: 1 sum1 = 552 sum2 =-858993415 (fixed value) 3 sum3 = 304 sum4 = 555 sum5 = 556 sum6 = 55 visible: 1. for a [n-1] + sum2 (a, -- n), GCC Compilation Is equivalent to a [n-1] + sum2 (a, n-1), while the VC compiler first subtract n, equivalent to a [N-2] + sum2 (a, n-1); 2. for a [-- n] + sum3 (a, n-1), the GCC compiler first subtract n, equivalent to a [n-1] + sum2 (a, N-2 ); the VC compiler is the same as it is. 3. different compilers have different processing sequence for expressions and auto-subtraction (or auto-increment) operators. Therefore, the straightforward and easy-to-understand programming formula (such as sum1) should be used to avoid auto-increment/auto-subtraction; 4. In recursive algorithms, if in sum1 is equivalent to while in sum6. 5. Check whether the compiler has any bugs when the syntax is correct but it runs abnormally. Finally, we will briefly mention the abnormal values of sum4 and sum2 under the two compilers. The analysis function call process shows that both outliers are caused by arrays out of bounds (gcc-sum4 a [10], vc-sum2 a [-1]). -The hexadecimal representation of-1076625666 is 0xbfd3fefe, which is obviously a Linux stack address, and the hexadecimal representation of-858993415 is 0xccccccf9, 0xcc is the initial value assigned by the system to uninitialized stack zone variables in the Debug mode of the VC compiler. In addition, because the stack address is a runtime concept, the result of sum4 may slightly change after each execution. The special placeholder value of 0xcc of VC is fixed, so the result of sum2 does not change with the number of executions. Slightly modify the test code: copy the Code 1 int main (void) 2 {3 int a [] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 4 int n = sizeof (a)/sizeof (a [0]); 5 printf ("sum1 = % d \ n", sum1 (, n); 6 printf ("sum2 = % d \ n", sum2 (a, n); 7 printf ("sum3 = % d \ n", sum3 (, n); 8 printf ("sum4 = % d \ n", sum4 (a, n); 9 printf ("sum5 = % d \ n", sum5 (, n); 10 printf ("sum6 = % d \ n", sum6 (a, n); 11 return 0; 12} if you copy the code, the GCC compiler will give different results: 1 sum1 = 552 sum2 = 553 sum3 = 304 sum4 = 645 sum5 = 55

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.