Thread: Assertion Failure

  1. #1
    JagWire
    Guest

    Assertion Failure

    Anyone know what an assertion failure is? My program is supposedly crashing from it and I have no idea why.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    An assertion failure is... welll... I'll let a search engine tell you.

    Basicly that sums it up. 'assert' is a macro used to cause your program to bomb out. In a nut shell, it's this:

    if( something == 0 ) exit( 0 );

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    4
    Hey quzah, thanks for answering JagWire's question. I actually had the same problem, but my return is -1 and not 0. I'm sure JagWire had an error message that popped up similar to this:

    Debug Assertion Failed!

    Program: C:\My Documents\...\Debug\project1.exe
    File: fgets.c
    Line: 60

    Expression: str !=NULL

    For information on how your program can cause an assertion
    failure, see the Visual C++ documentation on asserts.


    I was wondering if you had any further tips/suggestions. i'll be reading the search results you posted as well. I didn't have as much luck when I querried Google as you did.

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    4
    I've isolated the code that is giving me problems:

    Code:
      if(fgets(hex2, 45, fp) == NULL) 
    	{
          /* No more lines to read*/
          
    		#ifdef VARIED_LENGTH_STRINGS
    		copy = malloc (strlen (hex2)+1);
    			if (copy != NULL) 
    			{
    				free (copy);
    				copy = NULL;
    			} // end if
    	
    			#else
    				strcpy (copy, hex2);
    				Callback(copy);
    			#endif
    
    	} //end if
    the first line seems to be where my debugger starts acting funny. Any suggestions? I haven't been able to find anything I can understand and/or use in the search results.

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    66
    An assertion failure is when something that shouldn't happen does. Like passing a NULL pointer to fgets as the buffer.
    Code:
    char *buff = NULL;
    if (fgets (buf, BUFSIZ, stdin)){
      /* Yadda yadda yadda */
    This will cause an assertion failure in fgets, the failure is caused by a call to the assert macro and is a note to you that something very bad happened that shouldn't have. This call usually tests for a null pointer argument or negative buffer size that would cause problems with the function.
    Code:
    #ifdef NDEBUG
      #define assert(exp) ((void)0)
    #else
        /* Function prototype */
      void Assert (char *, int, char *);
        /* Macro definition */
      #define assert(exp) ((exp) ? (void)0 : \
        Assert (__FILE__, __LINE__, #exp))
    
      /* Later on */
      void Assert (char *file, int line, char *msg)
      {
        fprintf (stderr, "%s : Line %d %s"
          " -- assertion failed\n", file, line, msg);
        abort();
      }
    #endif
    In your code snippet, hex2 is probably null when you call fgets with it.

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    4
    I'm not sure hex2 is null at the start. This routine is placed directly after my main program, which right now simply calls a file open function. I think in the course of executing the routine in the snippet i posted earlier, hex2 gets set to NULL with the strcpy command:

    strcpy (copy, hex2);

    I'm pretty sure the first time I use hex2 in the program is during that for loop.

    Code:
    char		hex2[45];
    
    int ReadLines(const char *Filename, ReadLineFunc Callback) 
    {
      FILE *fp = fopen("filename", "r");
    
      int		l_return;
      
    	if(fp == NULL) 
    	{
    		fprintf(stderr,"Error, file is not found. \n");
    		return -1;
    	} //end if
    
      /* Read each line and print it out */
      for(m=1;m<addr2+1;m=m+1) 
      {
        if(fgets(hex2, 45, fp) == NULL) 
    	{
          /* No more lines to read*/
          
    		#ifdef VARIED_LENGTH_STRINGS
    		copy = malloc (strlen (hex2)+1);
    			if (copy != NULL) 
    			{	
    				free (copy);
    				copy = NULL;
    			} // end if 
    	
    			#else
    				strcpy (copy, hex2);
    				Callback(copy);
    			#endif
    
    	} //end if

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    66
    Oh yuck, I just took a good look at your code and it scares me. First, if fgets returns null then that means no new data has been added to hex2, so why do you postpone processing until then? The assertion problem is no doubt in your call to strcpy since if malloc succeeds you immediately free the memory and assign null to copy. Then you try to send the contents of hex2 to copy. Did you write this function? It would be much better if it were something more like the following.
    Code:
    int ReadLines(const char *Filename, ReadLineFunc Callback)
    {
      char *copy;
      FILE *fp = fopen(Filename, "r");
      
      if(!fp)
      {
        fprintf(stderr,"Error, file is not found.\n");
        return (EXIT_FAILURE);
      }
      
      /* Use fgets as the loop counter, sizeof is there
         for array bounds safety
      */
      while(fgets(hex2, sizeof (hex2), fp) != NULL)
      {
        copy = malloc (strlen (hex2)+1);
        /* If copy is NOT null */
        if (copy)
        {
          strcpy (copy, hex2);
          Callback(copy);
          /* Assuming you're done with copy, kill it */
          free (copy);
        }
      }
      return (EXIT_SUCCESS);
    }
    Maybe you should explain what this function does. Is it supposed to read the last line of the file and process it or read and process every line?

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    4
    i'm trying to read and process every line of a file which contains approx. 640 lines to be processed. I will try your code out and let you know what I come up with... thanks in advance Crimpy!!

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
    By talz13 in forum Windows Programming
    Replies: 2
    Last Post: 07-20-2004, 11:23 AM
  3. Assertion Failure
    By osal in forum C Programming
    Replies: 11
    Last Post: 06-03-2004, 10:25 PM
  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