Sign in to follow this  
donjonson

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

Recommended Posts

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[i] != 0)
		{
			cout<<char(i)<<": "<<chCount[i]<<endl;
		}
	}

	infile.close();
	}
	return 0;
}


[Edited by - donjonson on March 25, 2006 2:02:39 PM]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites

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[i] = '\0';

i=0;
while(chCount[i] && i < 256)
cout << chCount[i++];

cin.ignore();
}




[Edited by - simon10k on March 25, 2006 12:29:49 PM]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 =)*

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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};



Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this