myclass::myclass(queue<queue<char> > construction_info){
//why does this line crash?
queue<char> first_line_of_construction_info = construction_info.front();
construction_info.pop();
}
I am reading from text files (not generated by me so I can't change the format), into a queue of queue of char. It means lines of characters. And I process that info to generate the class. However, after working in a few debug messages I realized that the first time I am getting a bad_alloc on execute (the program initialized all myclasses from text files at startup) is this line in the code.
I'm new to working with C++ and my google-fu hasn't really helped me with this problem. Does anyone have any suggestions as to where I can start solve this crash?
Simply uncommenting the class constructor is letting my program work without any crashes, obviously without generating actually useful objects of course.
Using g++ with c++11 on linux.
Edit:
Here is the full code cut from the main file:
int initialize_classrooms(){
path p = "files/classrooms/";
//files of lines of queues of chars
//vector of vector of queue of char
vector<queue<queue<char> > > classroom_files;
if(exists(p)){
for (directory_entry& x : directory_iterator(p)){
queue<queue<char> > cur_file;
ifstream file(x.path().filename().string());
queue<char> cur_line;
char ch;
while (file >> noskipws >> ch) {
if(!isspace(ch)){
cur_line.push(ch);
}else if(ch == '\n'){
cur_file.push(cur_line);
cur_line = queue<char>();
}
}
classroom_files.push_back(cur_file);
cur_file = queue<queue<char> >();
file.close();
}
}else{
cout << "Classroom files are missing!" << endl;
return 1;
}
cout << "Got all the way to classroom creation" << endl;
int i = 1;
for(auto cf : classroom_files){
cout << "Number of loops: " << i << endl;
i++;
shared_ptr<classroom> cr = shared_ptr<classroom>(new classroom(cf));
}
cout << "Got past the classroom creation" << endl;
return 0;
}
bad_alloc
normally means you've run out of memory.std::queue<std::queue<char>>
in the first place? Why notstd::queue<std::string>
? Or even juststd::string
by itself? Why are usingstd::vector<std::queue>
instead ofstd::vector<std::string>
? Why are you reading the file char-by-char, instead of line-by-line (such as viastd::getline()
)? Does each individualchar
actually need to be processed individually? Or do you need to process lines instead? Do you really need multiple layers of queues? Or just a single list of lines in a file? This code is using a lot of overhead for something that should be trivial.