# Different output if I debug?

• 02-11-2010
rodrigorules
Different output if I debug?
I have a program that spits out nonsense when I run it, but it runs flawlessly if I run it step-by-step in a debugger with the same input.

any idea why this could be?

-------------
I am reluctant to paste long code since it might deter people who can help but, here it is..

(program finds path down graph with largest number sum)
(first line is # of graphs, 2nd is nxm size, then box of numbers is the graph)
input is:
1
2 2
1 1 6
9 1 1
1 1 1

output when debugging step-by-step : 11
output when running exe: 775639875

Code:

```#include <stdio.h> inline int max(int a,int b,int c) {     if(a>b)         if(a>c)             return a;         else             return c;     if(b>a)         if(b>c)             return b;         else             return c;     if(c>a)         if(c>b)             return c;         else             return b; return b; } void solve(int **grid,int rows,int cols) {     for(int j=0;j<rows;++j)     {         grid[j][0] = -9999;         grid[j][cols+1] = -9999;     }     int **dp = new int*[rows];     for(int i = 0; i < rows; i++)             dp[i] = new int[cols+2];     for(int i=0;i<=cols+1;++i)         dp[0][i] = grid[0][i];     for(int k=1;k<rows;++k)     {         for(int n=1;n<=cols;++n)         {             int a,b,c,largest;             a = dp[k-1][n-1]+grid[k][n];             b = dp[k-1][n]+grid[k][n];             c = dp[k-1][n+1]+grid[k][n];             largest = max(a,b,c);             dp[k][n] = largest;         }     }     int solution = -9999;     for(int i=1;i<=cols;++i)         if(dp[rows-1][i] > solution)             solution = dp[rows-1][i];     printf("%d\n",solution); } int main() {     int n;     scanf("%d",&n);     int **grid;     while(n--)     {         int rows,cols;         scanf("%d %d",&rows,&cols);         grid = new int*[rows];         for(int i = 0; i < rows; i++)             grid[i] = new int[cols+2];         for(int i=0;i < rows;++i)             for(int j=1;j <= cols;++j)                 {                     int in;                     scanf("%d",&in);                     grid[i][j]=in;                 }         solve(grid,rows,cols);     }     return 0; }```
Thanks a ton!

btw: i am 10000% sure the code works correctly since I received correct answer for this code already.

i am using code::blocks compiler and debugger
• 02-11-2010
anon
Your dp array gets only partially initialized, so you'd be using uninitialized values. In debug builds uninitialized variables may be initialized anyway.

Also you could easily change it to use std::vector, which would avoid memory leaks and the general verbosity of creating the arrays. (Example code takes advantage of vector only partly, now you won't need to pass the row and column count either.)

Commented out code includes a fix for your code.

Code:

```#include <stdio.h> #include <vector> #include <algorithm> typedef std::vector<int> Row; //or Column? typedef std::vector<Row> Grid; inline int max(int a,int b,int c) {     if(a>b)         if(a>c)             return a;         else             return c;     if(b>a)         if(b>c)             return b;         else             return c;     if(c>a)         if(c>b)             return c;         else             return b; return b; } void solve(Grid& grid /*int **grid*/,int rows,int cols) {     for(int j=0;j<rows;++j)     {         grid[j][0] = -9999;         grid[j][cols+1] = -9999;     }     Grid dp(rows, Row(cols + 2));     //int **dp = new int*[rows];     //for(int i = 0; i < rows; i++) {     //        dp[i] = new int[cols+2];     //        std::fill(dp[i], dp[i] + cols + 2, 0);     //}         for(int i=0;i<=cols+1;++i)         dp[0][i] = grid[0][i];     for(int k=1;k<rows;++k)     {         for(int n=1;n<=cols;++n)         {             int a,b,c,largest;             a = dp[k-1][n-1]+grid[k][n];             b = dp[k-1][n]+grid[k][n];             c = dp[k-1][n+1]+grid[k][n];             largest = max(a,b,c);             dp[k][n] = largest;         }     }     int solution = -9999;     for(int i=1;i<=cols;++i)         if(dp[rows-1][i] > solution)             solution = dp[rows-1][i];     printf("%d\n",solution); } int main() {     int n;     scanf("%d",&n);     //int **grid;     while(n--)     {         int rows,cols;         scanf("%d %d",&rows,&cols);         Grid grid(rows, Row(cols + 2));         //grid = new int*[rows];         //for(int i = 0; i < rows; i++)             //grid[i] = new int[cols+2];         for(int i=0;i < rows;++i)             for(int j=1;j <= cols;++j)                 {                     int in;                     scanf("%d",&in);                     grid[i][j]=in;                 }         solve(grid,rows,cols);     }     return 0; }```
• 02-11-2010
rodrigorules
wow thanks anon!
• 02-11-2010
jeffcobb
The other thing to watch for (not in your case but stick this under your hat; it is a similar situation): If you have a multithreaded app and it behaves like this (soils itself in release mode, works great under debugger) the reason is that the debugger will enforce single-threaded operation thus a race condition in release mode will almost certainly be covered up in the debugger...one of the fun things about multithreaded coding on anything more than a trivial example...