fbm_bcn
fbm_bcn

Reputation: 23

_ttoi win c++ assertion failure

i have this code:

BOOLEAN   Recurse = FALSE;
DWORD     NumPasses = 1;


int _tmain( int argc, TCHAR *argv[] ) 
{

    BOOL        foundFileArg = FALSE;
    int         i;

   if( argc < 2 ) {

        return Usage( argv[0] );
    }


    for( i = 1; i < argc; i++ ) {

        if( !_tcsicmp( argv[i], TEXT("/s") ) ||
            !_tcsicmp( argv[i], TEXT("-s") )) {

            Recurse = TRUE;

    } else if( !_tcsicmp( argv[i], TEXT("/p") ) ||
                   !_tcsicmp( argv[i], TEXT("-p") )) {

                   // assertion failure 
            NumPasses = argc > i ? _ttoi( argv[i+1] ) : 1;
            if( !NumPasses ) return Usage( argv[0] );
            i++;


        } else {

            if( foundFileArg ) return Usage( argv[0] );
            foundFileArg = TRUE;
        }
    }


    return 0;

}

i get assertion failure, Please suggest where the problem might be and where to look. Is it some problem with _ttoi function i'm using when it fails, if i have to allocate a buffer, how can i resolve it

thanks

Upvotes: 1

Views: 1296

Answers (2)

Mahmoud Fayez
Mahmoud Fayez

Reputation: 3459

this line

NumPasses = argc > i ? _ttoi( argv[i+1] ) : 1;

should be

NumPasses = argc > 1+i ? _ttoi( argv[i+1] ) : 1;

Upvotes: 1

Steve
Steve

Reputation: 169

Nick is right; don't forget that arrays start at zero in C/C++. If there are 5 elements, it means argv[0] to argv[4] are valid - not argv[5].

Upvotes: 0

Related Questions