C++ program gives different results in Debug and Release mode -


i'm doing algorithmic exercises , encountered strange problem. wrote code below solve problem http://main.edu.pl/en/archive/oi/21/pta. tested , looks works fine in debug mode, switch release (or submit site) gives wrong answers tests.

eg 9e.in (i uploaded test here: https://www.dropbox.com/s/ki4vfk2p5140xwo/pta9e.in?dl=0) answer debug build correct:

255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 

however, in release (and after submitting site) get:

543 530 530 530 530 530 543 530 530 530 543 530 530 543 530 530 530 543 530 530 543 530 530 530 530 

i have no idea can causing problem :/ code:

#define _crt_secure_no_warnings  #include <iostream> #include <cstdlib> #include <queue> #include <cmath> #include <stdio.h>  using namespace std;  int numoftrees = 0; int* trees = null;  int numofbirds = 0;  int computebird(int birdstamina) {     int tired = 0;      int position = 0;     int lasttree = 0;      while (true)     {         if (position >= numoftrees - 1)         {             return tired;         }          lasttree = trees[position];          int furthestgoodtree = -1;          int tallesttreepos = position + birdstamina;         if (tallesttreepos > numoftrees - 1)         {             tallesttreepos = numoftrees - 1;         }          int tallesttreesize = trees[tallesttreepos];          (int n = 1; n <= birdstamina; ++n)         {             int pos = position + n;              if (pos >= numoftrees)                 break;              if (trees[pos] < lasttree)             {                 if (trees[pos] >= trees[furthestgoodtree])                 {                     furthestgoodtree = pos;                 }             }              if (trees[pos] >= tallesttreesize)             {                 tallesttreesize = trees[pos];                 tallesttreepos = pos;             }         }          if (furthestgoodtree != -1)         {             position = furthestgoodtree;         }         else         {             position = tallesttreepos;             tired++;         }     } }  int main() {     scanf("%d", &numoftrees);      trees = new int[numoftrees + 2];      int val = 0;      (int n = 0; n < numoftrees; ++n)     {         scanf("%d", &val);         trees[n] = val;     }      scanf("%d", &numofbirds);      int* cache = new int[numoftrees + 2];      (int n = 0; n < numoftrees; ++n)     {         cache[n] = -1;     }      int results[30];      int birdstamina = 0;      (int n = 0; n < numofbirds; ++n)     {         scanf("%d", &birdstamina);          if (cache[birdstamina] != -1)         {             results[n] = cache[birdstamina];         }         else         {             int result = computebird(birdstamina);              cache[birdstamina] = result;             results[n] = result;         }     }      (int n = 0; n < numofbirds; ++n)     {         printf("%d\n", results[n]);     }      //system("pause");      return 0; } 

this code ought have assert added

       if (trees[pos] < lasttree)         {             assert( furthestgoodtree >= 0 );             if (trees[pos] >= trees[furthestgoodtree])             {                 furthestgoodtree = pos;             }         } 

so far can tell, condition data dependent, not forced other code , not forced local code.

when rely on condition being true , not locally obvious why should true, should have assert debug runs notice incorrect assumptions.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -