BUS error when reading input from file

This is a discussion on BUS error when reading input from file within the C Programming forums, part of the General Programming Boards category; hello to all, (*ignore title part about input file) I am not sure where the problem is in my code. ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    77

    BUS error when reading input from file

    hello to all, (*ignore title part about input file)

    I am not sure where the problem is in my code. Everything is compiling correctly.

    here is the code:

    Code:
    else if ((strcmp (command, "align")) == 0 )
                        {
                            printf("Align sequences ( not yet implemented)\n");
                            
                            int firstSeq = atoi(strtok(NULL, " \n\t")) ;  
                            int secondSeq = atoi(strtok(NULL, " \n\t")) ;  
                            
                            #define GAPSCORE -1 
    
                            // m = top sequence
                            // n = bottom sequence
                            
                            // Change bestScore setup to capture best score
                            // in outer column and row
                            // Note: this may not be the global bestScore
                            
                            typedef struct DPCell { char rowChar ; char colChar ;  int Score ; struct DPCell *traceBack ; int traceBackType; } DPCell ; 
                            
                            // Traceback type = 0 for vertical, 1 for horizontal, 2 for 
                            // diagonal, -1 undefined 
                            
                            DPCell **DPArray ;   // 2-D array
                          
                            
                            // collect sequences from seqTool (*Modify)
                            char *rowSeq = loadedSequences[firstSeq-1].data ; // top sequence (m) "left edge of matrix"
                            char *colSeq = loadedSequences[secondSeq-1].data ; // bottom sequence (n) "top edge of matrix"
                            
                            
                            // Add 1 for the first column that contains gap penalty ( = -1 )
                            int rowLength = strlen( rowSeq ) + 1 ; 
                            int colLength = strlen( colSeq ) + 1 ; 
                            
                            // Arrays to collect final alignment (Must be reversed!!)
                            char *rowAlign = (char *) malloc( (strlen(rowSeq) + strlen(colSeq) + 1) * sizeof( char ) ) ; 
                            char *colAlign = (char *) malloc( (strlen(rowSeq) + strlen(colSeq) + 1) * sizeof( char ) ) ; 
                            
                            // Allocate DP matrix 
                            DPArray = (DPCell **) malloc( rowLength  * sizeof( DPCell * ) ) ; 
                                
                                int i, j ; 
                            
                                for( i = 0 ; i < rowLength ; ++i ) 
                                    { 
                                        DPArray[i] = (DPCell *) malloc( colLength * sizeof( DPCell ) ) ; 
                                    }  
                            
                            // Initialize Upper left corner 
                                DPArray[0][0].rowChar = '\0' ; 
                                DPArray[0][0].colChar = '\0' ; 
                                DPArray[0][0].Score = 0 ; 
                                DPArray[0][0].traceBack = NULL ; 
                                DPArray[0][0].traceBackType = -1 ;
                            
                            // Left edge 
                                for( i = 1 ; i < rowLength ; ++i ) 
                                    { 
                                        DPArray[i][0].rowChar = toupper(rowSeq[i-1]) ; 
                                        DPArray[i][0].colChar = '\0' ; 
                                        DPArray[i][0].Score = i * GAPSCORE ; 
                                        DPArray[i][0].traceBack = & DPArray[i-1][0] ; 
                                        DPArray[i][0].traceBackType = 0 ; 
                                    } 
                            
                            // Top edge 
                                for( j = 1 ; j < colLength ; ++j ) 
                                    { 
                                        DPArray[0][j].rowChar = '\0' ;
                                        DPArray[0][j].colChar = toupper(colSeq[j-1]) ;
                                        DPArray[0][j].Score = i * GAPSCORE ; 
                                        DPArray[0][j].traceBack = & DPArray[j-1][0] ; 
                                        DPArray[0][j].traceBackType = 1 ; 
                                    } 
                            
                            // Process the array interior 
                                for( i = 1 ; i < rowLength ; ++i ) 
                                    { 
                                        for( j = 1 ; j < colLength ; ++j ) 
                                            { 
                                                // Simple match score ( +1 for match, 0 for mismatch)
                                                if ( DPArray[i][j].rowChar == DPArray[i][j].colChar  )
                                                    {
                                                        DPArray[i][j].Score = (DPArray[i-1][j-1].Score + 1) ;
                                                    }
                                                else
                                                    {
                                                        DPArray[i][j].Score = DPArray[i-1][j-1].Score ;
                                                    }
                                                        {
                                            
                                                            // Horizontal                 
                                                            if( DPArray[i][j-1].Score + GAPSCORE > DPArray[i][j].Score ) 
                                                                { 
                                                                    DPArray[i][j].Score = DPArray[i][j-1].Score + GAPSCORE ; 
                                                                    DPArray[i][j].traceBack = & DPArray[i][j-1] ; 
                                                                    DPArray[i][j].traceBackType = 0 ; 
                                                                } 
                                                            
                                                            // Vertical
                                                            else if( DPArray[i-1][j].Score + GAPSCORE > DPArray[i][j].Score ) 
                                                                { 
                                                                    DPArray[i][j].Score = DPArray[i-1][j].Score + GAPSCORE ; 
                                                                    DPArray[i][j].traceBack = & DPArray[i-1][j] ; 
                                                                    DPArray[i][j].traceBackType = 1 ;
                                                                }
                                                                
                                                            // Diagonal
                                                            else
                                                                {
                                                                    DPArray[i][j].traceBack = & DPArray[i-1][j-1] ; 
                                                                    DPArray[i][j].traceBackType = 2 ;
                                                                }
                                                        }
                                                }
                                    }
                                
                            struct { int score ; struct DPCell *address ;} bestRowCell ;
                            struct { int score ; struct DPCell *address ;} bestColumnCell ;
                            struct { int score ; struct DPCell *address ;} bestCell ; 
                                                
                            // Find the address of the highest value in the bottom row 
                            bestRowCell.score = 0 ;
                            
                                for(i = rowLength, j=1 ; j < (colLength-1) ; ++j )
                                    {
                                        if( DPArray[i][j].Score > DPArray[i][j+1].Score ) 
                                            {
                                                bestRowCell.score = DPArray[i][j].Score ;
                                                bestRowCell.address = & DPArray[i][j] ;
                                            }
                                        else
                                            {   
                                                bestRowCell.score = DPArray[i][j+1].Score ;
                                                bestRowCell.address = & DPArray[i][j+1] ;
                                            }
                                    }
                            
                            // Find the address of the highest value in the right-most column
                            bestColumnCell.score = 0 ;
                            
                                for(i = 1, j= colLength ; i < (rowLength-1) ; ++i )
                                    {
                                        if( DPArray[i][j].Score > DPArray[i][j+1].Score ) 
                                            {
                                                bestColumnCell.score = DPArray[i][j].Score ;
                                                bestColumnCell.address = & DPArray[i][j] ;
                                            }
                                        else
                                            {   
                                                bestColumnCell.score = DPArray[i][j+1].Score ;
                                                bestColumnCell.address = & DPArray[i][j+1] ;
                                            }
                                    }
                                
                            // Take values for bestCell
                            bestCell.score = 0 ;
                            
                                if(bestColumnCell.score > bestRowCell.score)
                                    {
                                        bestCell.score = bestColumnCell.score ;
                                        bestCell.address = bestColumnCell.address ;
                                    }
                                else
                                    {
                                        bestCell.score = bestRowCell.score ;
                                        bestCell.address = bestRowCell.address ;
                                    }
                                    
                            struct DPCell *currentTraceBack = bestCell.address ;
                            
                            int alignPos = 0 ; 
                            while( currentTraceBack ) 
                                { 
                                    if( currentTraceBack->traceBackType == 2 ) 
                                        { 
                                            // Diagonal 
                                            
                                            rowAlign[alignPos] = currentTraceBack->rowChar ; 
                                            colAlign[alignPos] = currentTraceBack->colChar ; 
                                            ++alignPos ; 
                                         } 
                                   
                                    else if( currentTraceBack->traceBackType == 1 )
                                        {
                                            //  Horizontal
                                            
                                            rowAlign[alignPos] = '-' ;
                                            colAlign[alignPos] = currentTraceBack->colChar ;
                                            ++alignPos ; 
                                        }
                            
                                    else if( currentTraceBack->traceBackType == 0 ) 
                                        { 
                                            // Veritcal 
                                            
                                            rowAlign[alignPos] = currentTraceBack->rowChar ; 
                                            colAlign[alignPos] = '-' ; 
                                            ++alignPos ; 
                                        }
                                } 
                                        
                            // Reverse the alignments 
                            
                            //char topSequence[] = reverse(rowAlign) ;
                           // char bottomSequence[] = reverse(colAlign) ;
                            
                            // Print sequences
                            
                            //printf("&#37;c\n\n",topSequence) ;
                            //printf("%c\n",bottomSequence) ;
                           
                           printf("%c\n\n",rowAlign) ;
                           printf("%c\n",colAlign) ;
                           
                        }
    Any suggestions?

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You need to figure out how to post a meaningful question.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    77
    I am receiving a "Bus error" when i am running my program. It occurs somewhere in the posted code. Can anyone see a possible source in the posted code that may cause a "Bus error"?

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    It occurs somewhere in the posted code.
    code is too long - start stripping lines of the code
    for example putting several lines into
    Code:
    #if 0
    /* these lines will not be compiled */
    #endif
    till you locate the sourse of your problem

    do not cast malloc - read FAQ, check the return values of strtok, malloc etc before using this pointer - you can be trying to access null-pointers
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    If you run it in the debugger, you should find out which line is the problem at least.

    A bus error is usually the result of trying to access mis-aligned data. Are you doing anything odd with pointer arithmetic?

    Code:
                            struct { int score ; struct DPCell *address ;} bestRowCell ;
                            struct { int score ; struct DPCell *address ;} bestColumnCell ;
                            struct { int score ; struct DPCell *address ;} bestCell ;
    Use a typedef or a struct declaration if you really want these 3 variables to have the same type.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >                        DPArray = (DPCell **) malloc( rowLength  * sizeof( DPCell * ) ) ;
    No need for the cast:
    Code:
                            DPArray = malloc( rowLength  * sizeof( DPCell * ) ) ;
    Code:
    >                                    DPArray[i] = (DPCell *) malloc( colLength * sizeof( DPCell ) ) ;
    Same here:
    Code:
                                        DPArray[i] = malloc( colLength * sizeof( DPCell ) ) ;

    Code:
    >                            for(i = rowLength, j=1 ; j < (colLength-1) ; ++j )
    >                                {
    >                                    if( DPArray[i][j].Score > DPArray[i][j+1].Score )
    The value of i is out-of-bounds of the array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  3. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  5. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM

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