Thread: Assertion Failure

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

    Assertion Failure

    I'm using Microsoft Visual Studio 2005 for Windows XP.

    I have my program compiling, but when I run it I get 2 errors from the
    Microsoft Visual C++ Dbug Library in the microsoft file isctype.c. Both errors are the same - Expression: (unsigned)(c + 1) <= 256. The errors occur on the following lines where _ASSERTE occurs in isctype.c:

    Code:
    #if defined (_DEBUG)
    extern "C" int __cdecl _chvalidator(
            int c,
            int mask
            )
    {
            _ASSERTE((unsigned)(c + 1) <= 256);
            return _chvalidator_l(NULL, c, mask);
    }
    
    extern "C" int __cdecl _chvalidator_l(
            _locale_t plocinfo,
            int c,
            int mask
            )
    {
        _LocaleUpdate _loc_update(plocinfo);
    
        _ASSERTE((unsigned)(c + 1) <= 256);
        if (c >= -1 && c <= 255)
        {
            return (_loc_update.GetLocaleT()->locinfo->pctype[c] & mask);
        }
        else
        {
            return (_loc_update.GetLocaleT()->locinfo->pctype[-1] & mask);
        }
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Please post the code you wrote that causes this error.
    My best code is written with the delete key.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    My guess is you did some ctype test on EOF.
    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.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    80
    Thanks for getting back to me. Here is the code that's causing the error:
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include "molecule.h"
    #include "smiles.h"
    #include "smierr.h"
    
    main( int argc, char *argv[] )
    {
       char buf[200];
       char s[MAX_SMI];
       char sf[MAX_SMI];
       int Error, ErCode;
       int N_Frag, Found;
       MOLECULE m;
       RINGS r;
       long id;
       FILE *ff, *fs, *fo;
    
       if ( argc != 4 ) {
          printf( "USAGE: fragment <smiles-file> <fragment-file> <output-file>\n" );     
    	  exit( 1 );
       }
       
       if ( (fs = fopen( argv[1], "r" )) == NULL ) {
          printf( "Unable to open SMILES file %s\n", argv[1] );
          exit( 1 );
       }
       if ( (ff = fopen( argv[2], "r" )) == NULL ) {
          printf( "Unable to open FRAGMENT file %s\n", argv[2] );
          exit( 1 );
       }
       if ( (fo = fopen( argv[3], "w" )) == NULL ) {
          printf( "Unable to open OUTPUT file %s\n", argv[3] );
          exit( 1 );
       }
    
       printf( " Processed: " );
       fgets( buf, 200, fs );      /* Retrieve line from smiles file */
       
       sscanf( buf, "%ld %s", &id, s);
       while ( !feof( fs ) ) {     
    	   if ( (Error = InSmi( s, &m, &ErCode, 0 )) != 0 ) {
             printf( "SMILES ERROR for %ld", id );
             getch();
          } else if ( (Error = ChkVal( &m )) != 0 ) {
             printf("VALENCE ERROR for %ld", id );
             getch();
          } else {
    	 FindRings(&m,&r);
             Found = 0;
             fscanf( ff, "%s", sf );
             while ( (!feof( ff )) && !Found ) {
    	    N_Frag = EV_Num( sf, &m, &r );
                if ( N_Frag > 0 ) {
                   fprintf( fo, "%10ld|%s\n", id, s );
                   Found = 1;
                } else if ( N_Frag < 0 ) {
                   printf( "FRAGMENT ERROR\n" );
                   exit( 1 );
                }
                fscanf( ff, "%s", sf );
             }
             rewind( ff );
          }
          printf( "Processing %7ld\n", id );
          fgets( buf, 200, fs );      /* Retrieve line from smiles file */
          sscanf( buf, "%ld %s", &id, s);
    
       }
       fclose( fs );
       fclose( ff );
       fclose( fo );
      
       exit( 0 );
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    But that's not all the code.

    1. Compile it all with debug.
    2. Run the code in the debugger.
    3. When you get an assert, drop into the debugger and get a stack trace.
    4. Follow the stack backwards until you get into your code.
    5. Explore around there to work out why you're passing an out of range value.

    Also, read the FAQ on why feof() is bad in control loops.
    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.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    80
    Thanks Salem, that gets me going in the right direction.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File I/O Assertion Failure in VS2008
    By clegs in forum C Programming
    Replies: 5
    Last Post: 12-25-2008, 04:47 AM
  2. Debug Assertion failure problem
    By uldaman in forum C++ Programming
    Replies: 8
    Last Post: 01-21-2008, 02:22 AM
  3. debug assertion failure
    By talz13 in forum Windows Programming
    Replies: 2
    Last Post: 07-20-2004, 11:23 AM
  4. Assertion failure while creating window
    By roktsyntst in forum Windows Programming
    Replies: 0
    Last Post: 02-10-2003, 08:18 PM
  5. Assertion Failure
    By drdroid in forum Game Programming
    Replies: 9
    Last Post: 01-04-2003, 07:02 PM