C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-17-2007, 12:57 PM   #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!
natmas is offline   Reply With Quote
Old 07-17-2007, 01:00 PM   #2
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,629
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, etc.

New project: nort
dwks is offline   Reply With Quote
Old 07-17-2007, 01:15 PM   #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...
natmas is offline   Reply With Quote
Old 07-17-2007, 01:17 PM   #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: %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)
natmas is offline   Reply With Quote
Old 07-17-2007, 01:30 PM   #5
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,629
Quote:
(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("%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, etc.

New project: nort
dwks is offline   Reply With Quote
Old 07-17-2007, 03:02 PM   #6
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
> 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.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 07-17-2007, 03:07 PM   #7
Registered User
 
Join Date: Jan 2005
Posts: 7,137
Especially since a relative path is used and the current directory when running through VC++ isn't necessarily what you'd expect.
Daved is offline   Reply With Quote
Old 07-17-2007, 04:28 PM   #8
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,629
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, etc.

New project: nort
dwks is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:20 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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