Visual Studio 2005 Debug Assertion Failed

This is a discussion on Visual Studio 2005 Debug Assertion Failed within the C++ Programming forums, part of the General Programming Boards category; it says: ---- Debug Assertion Failed! Program:... File: fgets.c Line: 57 Expression: ( str != NULL ) ---- the really ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    3

    Visual Studio 2005 Debug Assertion Failed

    it says:
    ----
    Debug Assertion Failed!

    Program:...
    File: fgets.c
    Line: 57

    Expression: ( str != NULL )
    ----

    the really frustrating thing is that this exact same code works on the two other computers i tried to run it on. i have another program that's giving a very similar error except its a

    (stream != NULL)

    written next to Expression, and of course the file and line are different because it's a different program. has anybody run into this issue, where the code works fine on other people's computers but not on your own with a very similar error? any idea how to fix (Without re-installing preferably... although if that will definitly work i am certainly open to suggestions)

    please help!

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    It's probably an error in the code; perhaps it does something undefined or uses an uninitialized variable or something. It might work on one computer, but that doesn't mean it will work everywhere, as you have seen. Re-installing likely wouldn't help at all.

    From the name of the source file, perhaps you're passing NULL as the first parameter to fgets()?

    Of course, it's possible there's something up with your compiler as well. You'd really have to post the code for anyone to tell.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    3
    hokay, i'll post the code, because i'm pretty sure there's not an error, but i want to make sure for real before going through the hassle of getting all that stuff off of my computer and back on again...

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    3
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void textToMatrix(FILE* file, long double *** dest, int* dataRows, int* dataCols){
    
    	char delim = ' ';	//change for different delimiter
    
    	char buf[1024];	//buffer for fgets
    printf("Reading File... \n");
    	char* line = fgets(buf, 1024, file);
    
    //	find number of columns of data
    	int cols = 0;
    	char *tmp = line;	//SHALLOW copy of line
    
    printf("Calculating number of columns...	");
    	while(*tmp == ' ') {	//move over initial white space
    		tmp++;
    	}
    	while(*tmp != '\0') {
    		if(*tmp == delim){
    			cols++;
    			while( *tmp == delim){	//skip extra delimeters
    				tmp++;
    			}
    		} else {
    		tmp++;
    		}
    	}
    printf("done! \n");
    printf("   cols: &#37;i\n",cols);
    
    //	Populate matrix with data from file
    printf("Populating matrix with data...		\n\n");
    	long double** data = (long double**)malloc(sizeof(long double *));
    	int rLen = 0;	//Number of rows in data matrix
    
    	while( line != NULL) {
    	//make new row
    		rLen++;
    		data = (long double**)realloc(data,rLen*sizeof(long double*));
    		data[rLen-1] = (long double*)calloc(cols,sizeof(long double));
    
    	//populate with data
    		char** endptr = &line;
    		for(int i = 0; i<cols; ++i) {
    			data[rLen-1][i] = (long double)strtod(line, endptr);
    			//printf("%5f ",data[rLen-1][i]);
    		}
    		//printf("\n");
    		line = fgets(buf,1024,file);
    	}
    printf("done! \n");
    printf("rLen: %d\n", rLen);
    printf("cols: %d\n", cols);
    	*dataRows = rLen;
    	*dataCols = cols;
    	*dest = data;
    }
    
    int main() {
    	FILE* file = fopen("test.txt","r");
    
    	long double** data;
    	int rows;
    	int cols;
    	textToMatrix(file, &data, &rows, &cols);
    	fclose(file);
    	printf("rows: %d, cols: %d\n",rows, cols);
    	for(int i=0; i<rows; ++i) {
    		for(int j=0; j<cols; ++j) {
    			printf("%1f ",data[i][j]);
    		}
    		free(data[i]);
    		printf("\n");
    	}
    	free(data);
    
    	getchar();
    	return 0;
    }
    well, that's it start to finish (no, there's no header file, bad me)

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    (no, there's no header file, bad me)
    A header file can complicate things more than it's worth for smaller programs, especially if you only have one or two functions besides main(). There's nothing wrong with not using one.

    Code:
    	//populate with data
    		char** endptr = &line;
    		for(int i = 0; i<cols; ++i) {
    			data[rLen-1][i] = (long double)strtod(line, endptr);
    			//printf("&#37;5f ",data[rLen-1][i]);
    		}
    		//printf("\n");
    Maybe you want something like this?
    Code:
    	//populate with data
    		char* endptr = line;
    		for(int i = 0; i<cols; ++i) {
    			data[rLen-1][i] = (long double)strtod(endptr, &endptr);
    			//printf("%5f ",data[rLen-1][i]);
    		}
    		//printf("\n");
    If you don't do that, you might as well pass NULL for the second parameter, and you'd be getting the same number for each element in data[rLen-1][].

    Also, perhaps you want strtold(). http://www.opengroup.org/onlinepubs/...ns/strtod.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    > From the name of the source file, perhaps you're passing NULL as the first parameter to fgets()?
    Actually, it's far more likely to be the 3rd parameter, namely the FILE* handle which originates from the UNCHECKED result of fopen()
    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.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Especially since a relative path is used and the current directory when running through VC++ isn't necessarily what you'd expect.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Yes, that's true -- my guess was before I saw the code, and then I didn't revise it.

    Checking the return value of fopen() against NULL to see if it failed is a good idea.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Help linked lists Visual Studio 2005
    By snap_Dragon555 in forum C++ Programming
    Replies: 4
    Last Post: 11-30-2006, 03:53 AM
  3. using classes
    By krazykrisi in forum C++ Programming
    Replies: 9
    Last Post: 11-22-2006, 09:41 AM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 07:13 PM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM

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