Jump to content
  • Advertisement
Sign in to follow this  
donjonson

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

This topic is 4500 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 != 0)
		{
			cout<<char(i)<<": "<<chCount<<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
Advertisement
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 = '\0';

i=0;
while(chCount && 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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!