question

xianli-6795 avatar image
0 Votes"
xianli-6795 asked SamMueller-0712 commented

In VS2019, Why two diffrent results in Debug and Release about a code

 #include <stdio.h>
    
 int count_num = 0;
 int count(int n) {
     if (n == 1) {
         return count_num;
     }
     if (n % 2 == 0) {
         n = n / 2;
     }
     else {
         n = (3 * n + 1) / 2;
     }
     count_num++;
     count(n);
       
 }
 int main(void) {
     int n = 0;
     scanf("%d", &n);
     printf("%d", count(n));
     return 0;
 }

In Debug, the result is right but the Release is wrong. Why?
125223-qq%E6%88%AA%E5%9B%BE20210821150114.png125170-qq%E6%88%AA%E5%9B%BE20210821150001.png


msc-essentials
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

KenDevguy-2340 avatar image
0 Votes"
KenDevguy-2340 answered SamMueller-0712 commented

xianli-6795

I think you need to add return count(n); not just count(n);
When I did that, 3 gave me 5 (the correct answer) in both debug and release builds.

The reason, I suspect, is that in debug builds, unused memory is initialised to 0 by the runtime as the program starts.
For release builds, this cleaning of memory is not performed. I think. Best to check this in the documentation.
I just thought I would give you a quick answer - I'm not %100 sure of that

Anyway, hope that helps. scanf kept giving me a warning error until I checked its return value btw.

Ken

· 9
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Compiling the posted code gives a warning message - warning C4715: 'count': not all control paths return a value

I expect that causes the optimized code created in a release build to produce the unexpected result. By using "return count(n)" the warning is eliminated and the optimized code then produces the expected result.

A slightly altered version of the posted code that does not return a value from the count function -

 #define _CRT_SECURE_NO_WARNINGS
 #include <stdio.h>
    
 int count_num = 0;
 void count(int n) {
     if (n == 1) {
         return;
     }
     if (n % 2 == 0) {
         n = n / 2;
     }
     else {
         n = (3 * n + 1) / 2;
     }
     count_num++;
     count(n);
 }
 int main(void) {
     int n = 0;
     scanf("%d", &n);
     count(n);
     printf("%d", count_num);
     return 0;
 }
2 Votes 2 ·

Yep, that's the warning I got too, leading me to add the return statement. :-)
Here is the final code. Thanks.

 int count_num = 0;
    
 int count(int n)
 {
      if (n == 1)
      {
         return count_num;
      }
      if (n % 2 == 0)
      {
         n = n / 2;
      }
      else
      {
         n = (3 * n + 1) / 2;
      }
     count_num++;
     return count(n);
    
 }
 int main(void)
 {
     int res;
     int n = 0;
    
     res = scanf("%d", &n);
     printf("%d", count(n));
     return 0;
 }
0 Votes 0 ·

@ KenDevguy-2340 thanks! Had a similar problem but was concerned with this particular part of code:

  int main(void) {
      int n = 0;
      scanf("%d", &n);
      count(n);
      printf("%d", count_num);
      return 0;
  }

Also I was surpised that scanf works in a different way compared to Python


3 Votes 3 ·

And the moral of the story is to make sure to pay attention to compiler warning messages. :)

2 Votes 2 ·
Show more comments

I got it, thank you!

0 Votes 0 ·