   string crashing for no apparent reason
  I have a text file that I read in; the first line in the file is the number of entries to be read in. Previously the file had 76 entries, but I just added 63 more, so now it has 139. The problem is that if I change the first line of the file to be 139, my program crashes - it gives me an unhandled exception in alloc.c upon trying to create a string to pass into my Question constructor. It seems that any value greater than 43 causes the program to crash now.void CQuestionDialog::LoadQuestions(string file) {int count;ifstream inFile;inFile.open(file.c_str());inFile >> count;if( count <=0 )return;int spot;string q;getline(inFile, q);for(; count>0; count--) {string a, t;do {getline(inFile, t);q += t;spot = q.find("-|");}while( spot < 0 );t.clear();do {getline(inFile, t);a += t;spot = a.find("-|");spot = spot;}while( spot < 0 );vQuestList.push_back(Question(q, a));q.clear();}fileName = file;inFile.close();}
  Well, I'll look over the code some more in a minute, but my first suggestion would be to look into stringstreams, which just helped me immensely with similar problems.  They help with reading/parsing raw data into a certain format, casting to and from strings and other datatypes, and are old enough to drive, if you aren't [smile]   ;  Well I'm not having much luck with them either. Now on this it could be that I have no idea how to use sstreams, but either way it still crashes.int length;char* buffer;ifstream is;is.open(file.c_str());is.seekg(0, ios::end);length = is.tellg();is.seekg(0, ios::beg);buffer = new char[length];is.read(buffer,length);is.close();stringstream ins(buffer);Once I try to pass the buffer into the sstream constructor, my program bails out in a new() function citing a bad_alloc. And yes, I can drive.   ;  OK, so I guess your goal is to read in a text file, and get each line individually?  Here's what I would do, which I will give to you in the hopes that it will demonstrate how to use stringstreams (I tested it, and it works for me).  It loads a file into a stringstream, and then displays it line by line.  You could just as easily store it in a string, or whatever.  Hope this helps:void LoadAndDisplayFile(const char * filename){    std::ifstream fin(filename, std::ifstream::in);    std::stringstream ss;    char * buffer = new char[256];    int remaining, offset;    while (1)    {   char c = fin.get();   if (fin.good())  ss.put(c);   else  break;    }    do    {   ss.getline(buffer, 256);   offset = ss.tellg();   ss.seekg(0, std::ios::end);   remaining = ss.tellg();   ss.seekg(offset, std::ios::beg); if (remaining > 0)   {  std::cout << "'" << buffer << "'\n";   }  } while (remaining > 0);};  I don't see any outright errors in the OPs code (although there are a number of issues).  Using a stringstream in this instance is a complete waste.  Anything that can be done on a stringstream can be done on a file stream.  No offense but silverphyre673's code is horrendously inefficient (and it also leaks the character buffer).I suspect the actual problem lies in other code corrupting the heap/stack.  Probably caused by an attempt to use a dangling pointer or an array access out of bounds.  What does your debugger tell you?Some issues with Sfpiano's code:std::string::find returns a std::string::size_type, which is an unsigned integral type.  You assign this to a signed int and then test if it is less than zero.  This will probably work, but is dangerous.  Much better to store the result in a variable of type std::string::size_type and compare against std::string::npos.Lack of error checking.  If the file does not contain "-|" then the loop will never terminate.I would probably have written that code something like:std::string readQuestionOrAnswer(std::ifstream & inFile){std::string result;std::string line;do{std::getline(inFile, line);result += line;} while (inFile.good() && line.find("-|") == std::string::npos);if (!inFile){// error handling}return result;}void QuestionDialog::loadQuestions(std::string file){std::ifstream inFile(file.c_str());if (!inFile){// error handling}int numberOfQuestions;inFile >> numberOfQuestions >> std::ws;if (!inFile || numberOfQuestions <= 0){// error handling}while (numberOfQuestions > 0){std::string question = readQuestionOrAnswer(inFile);std::string answer = readQuestionOrAnswer(inFile);questionList.push_back(Question(question, answer));--numberOfQuestions;}fileName = file;}Enigma   ;Quote:Original post by SfpianoWell I'm not having much luck with them either. Now on this it could be that I have no idea how to use sstreams, but either way it still crashes.int length;char* buffer;ifstream is;is.open(file.c_str());is.seekg(0, ios::end);length = is.tellg();is.seekg(0, ios::beg);buffer = new char[length];is.read(buffer,length);is.close();stringstream ins(buffer);Once I try to pass the buffer into the sstream constructor, my program bails out in a new() function citing a bad_alloc. And yes, I can drive.As far as I know, using tellg on a file not opened in binary mode is undefined behaviour. I think it's returning -1 for you in this case.   ;Quote:Original post by EnigmaI don't see any outright errors in the OPs code (although there are a number of issues).  Using a stringstream in this instance is a complete waste.  Anything that can be done on a stringstream can be done on a file stream.  No offense but silverphyre673's code is horrendously inefficient (and it also leaks the character buffer).I suspect the actual problem lies in other code corrupting the heap/stack.  Probably caused by an attempt to use a dangling pointer or an array access out of bounds.  What does your debugger tell you?Some issues with Sfpiano's code:std::string::find returns a std::string::size_type, which is an unsigned integral type.  You assign this to a signed int and then test if it is less than zero.  This will probably work, but is dangerous.  Much better to store the result in a variable of type std::string::size_type and compare against std::string::npos.Lack of error checking.  If the file does not contain "-|" then the loop will never terminate.I would probably have written that code something like:*** Source Snippet Removed ***EnigmaYeah, I forgot the delete - oops [smile].  It's not hard to load a text file in line by line - it just depends on what he wants to do with what he's loaded.  I wouldn't ordinarily use a stringstream, except that I kind of got the impression that he wanted to do some sort of casting to different data types with what he loaded, and I wanted to give a demo on how to use a stringstream without crashing the program.  Just use Enigma's code, if it suits you.   ;  Simplify your data format and you can simplify your design. Simplify your design and you can simplify your code. Simple code has this neat habit of working.First, remove the count from the beginning (you don't need it anyway, since you're using a vector and the file doesn't get used anywhere else, so presumably there's nothing else in there), and set the delimiter to be a single character. Then:void QuestionDialog::LoadQuestions(const string& filename) {  string items[2];  ifstream inFile(filename.c_str()); // that's what this ctor is there for :P  final int QUESTION = 0;  final int ANSWER = 1;  final char delimiter = '|'; // or whatever you choose to use. Why not just \n?  int currentItem = QUESTION;    while (getline(inFile, items[currentItem], delimiter)) {    if (currentItem == ANSWER) { myQuestions.push_back(Question(items[0], items[1])); currentItem = QUESTION;    } else { currentItem = ANSWER;    }  }}By the way, you don't need to .clear() strings before re-reading into them; getline() will overwrite the buffer just fine and there are no memory management problems (that's why you're using them in the first place).   