Different output if I debug?

This is a discussion on Different output if I debug? within the C++ Programming forums, part of the General Programming Boards category; I have a program that spits out nonsense when I run it, but it runs flawlessly if I run it ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    266

    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
    Last edited by rodrigorules; 02-11-2010 at 05:12 AM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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;
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    wow thanks anon!

  4. #4
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    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...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  2. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 03:29 PM
  3. Replies: 17
    Last Post: 10-23-2008, 10:10 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 06:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21