Skip to main content
GameDev.net gamedev.net
🔒 Locked

I have a runtime error that i cant figure out (SOLVED)

Started by donjonson Mar 25, 2006 at 10:16 AM 13 replies 2.8k views
Original Post
donjonson
donjonson
the runtime error I get is Run-Time Check Failure #2 - Stack around the variable 'chChount' was corrupted. I cant see anything wrong with what I am doing here can you? here is my code

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
	ifstream infile;
	string buffer;
	char chbuf;

	long chCount[256]= {0};
	//memset(chCount, 0, 256 * sizeof(int));

	cout<<"please entere a filename to analyze: ";
	getline(cin, buffer);
	infile.open(buffer.c_str(), ios::in);

	if(infile.is_open())
	{
	while(!infile.eof())
	{

		chbuf = infile.get();
	
		chCount[chbuf]++;
	}

	for(int i =0;i<256;i++)
	{
		if(chCount != 0)
		{
			cout<<char(i)<<": "<<chCount<<endl;
		}
	}

	infile.close();
	}
	return 0;
}


[Edited by - donjonson on March 25, 2006 2:02:39 PM]
These tears..leave scars...as they run down my face.
jonahrowley
jonahrowley
Does istream::get() return EOF on end of file? If so, the constant for an integer EOF will be an invalid character, either > 265 or < 0. Either way, you're writing outside your array bounds. istream::get() returns an int, not a char, so this is a possibility.
dave
dave
Hey bud,

At first guess i would say that it is because you have a char array of length 256 but are only setting the first element, upon initialisation.

Dave
jonahrowley
jonahrowley
Quote:
Original post by Dave
Hey bud,

At first guess i would say that it is because you have a char array of length 256 but are only setting the first element, upon initialisation.

Dave


That wouldn't cause an overflow. It would cause incorrect results though..
simon10k
simon10k
while(!infile.eof()){	chbuf = infile.get();		chCount[chbuf]++;}//I think the error is there. Try this...int i=0;while(!infile.eof())		chCount[i++] = infile.get();


edit:

I fixed it for you =)

#include <iostream>#include <string>#include <fstream>using namespace std;int main(){	cout<<"please entere a filename to analyze: ";	string buffer;		getline(cin, buffer);	ifstream infile(buffer.c_str());		if(infile.is_open() == false)		return 1;		char chCount[256];	int i = 0;	while(!infile.eof())						chCount[i++] = infile.get();		chCount = '\0';	i=0;	while(chCount && i < 256)		cout << chCount[i++];		cin.ignore();}


[Edited by - simon10k on March 25, 2006 12:29:49 PM]
-----------------------------Language: C++API: Win32, DirectXCompiler: VC++ 2003
Kylotan
Kylotan
You're reading into a char, which you shouldn't do. You should read into an int. This is because you're not guaranteed to get just a char. In fact, you may receive 'EOF' which is a value that may not fit into a char.

Secondly, the range of char is -128 to 127. You may therefore read in a value that doesn't fit in your array. In fact it would end up fitting before your array.

So what you should do, is read into an int, check it's not EOF, then cast it to an unsigned char before using it as an array index.
Iftah
Iftah
try changing "char chbuf" to "unsigned char chbuf"
jonahrowley
jonahrowley
Simon10k: That isn't what he was trying to do. In fact, that code is even more broken as it does NO bounds checking and will almost certainly overflow and probably crash. This is a perfect example of how NOT to read data from a file.
simon10k
simon10k
Whatever... This is a messy example to start with. I just noticed tho, that your right! he was trying to count the letters. *feels stupid =)*
-----------------------------Language: C++API: Win32, DirectXCompiler: VC++ 2003
donjonson
donjonson
Thaks for all your help.

and yes I am trying to count the characters in the file.

and it works perfectly. The error doesn't come until the program reaches literally the end. In debug it breaks after the closing perenthasis of main().
I think you were right when you said that I am trying to put a non char into the chbuf. I changed the section in question to the following and it fixed it.

while(!infile.eof())	{		chbuf = infile.get();			if(unsigned char(chbuf) <256)		chCount[unsigned char(chbuf)]++;	}



and instead of saying "this is a perfect example of how not to do something. why dont you give the right example. This is the for beginners message board after all. why post here if you not a beginner or not someone who would like to help beginners?
These tears..leave scars...as they run down my face.
donjonson
donjonson
Quote:
Original post by Dave
Hey bud,

At first guess i would say that it is because you have a char array of length 256 but are only setting the first element, upon initialisation.

Dave


actually the following does initialize every element in the array to 0 or whatever you put in the perens.
just FYI. :)

long chCount[256]= {0};
These tears..leave scars...as they run down my face.
jonahrowley
jonahrowley
Quote:
and instead of saying "this is a perfect example of how not to do something. why dont you give the right example.


I was talking about simon10k's code, and I don't think I need to provide an example of how to read data from a file into a buffer.
donjonson
donjonson
my mistake sorry :)
These tears..leave scars...as they run down my face.
Kylotan
Kylotan
Quote:
Original post by donjonson
Thaks for all your help.

and yes I am trying to count the characters in the file.

and it works perfectly.


By the way, you still have a couple of subtle bugs, that you may never notice but I'll share anyway.

.get can return EOF, as I said earlier. However, by that point you have already read in that value, and you'd try to count it in your array, which is not generally what you want to do.

Also, because you cast the value to an unsigned char, there is no point comparing them against 256, because every unsigned char is less than 256. So your if statement achieves nothing. One better way to write such a loop would be like so:
int chbuf;while((chbuf = infile.get()) != EOF){    ++chCount[unsigned char(chbuf)];}


Note that I also changed it to use pre-increment instead of post-increment. It's a good habit to get into using pre-increment where possible because it can be quicker than post-increment on some objects.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.