Anyone know what an assertion failure is? My program is supposedly crashing from it and I have no idea why.
Anyone know what an assertion failure is? My program is supposedly crashing from it and I have no idea why.
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.
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.
I've isolated the code that is giving me problems:
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.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
An assertion failure is when something that shouldn't happen does. Like passing a NULL pointer to fgets as the buffer.
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:char *buff = NULL; if (fgets (buf, BUFSIZ, stdin)){ /* Yadda yadda yadda */
In your code snippet, hex2 is probably null when you call fgets with it.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
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
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.
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?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); }
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!!