2
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;
}
6
  • 1
    How big is the queue? bad_alloc normally means you've run out of memory. May 4, 2017 at 1:43
  • How do you know the queue has something in it? Can you get the size of the queue before trying to get an item? May 4, 2017 at 1:44
  • @PaulRooney I have added the generation code
    – kittydoor
    May 4, 2017 at 1:48
  • @JerryJeremiah oh right, construction_info.empty() does equal true. I'm looking into the generation code to see why.
    – kittydoor
    May 4, 2017 at 1:48
  • Why are you using std::queue<std::queue<char>> in the first place? Why not std::queue<std::string>? Or even just std::string by itself? Why are using std::vector<std::queue> instead of std::vector<std::string>? Why are you reading the file char-by-char, instead of line-by-line (such as via std::getline())? Does each individual char 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. May 4, 2017 at 2:05

1 Answer 1

0

If the goal is only to read the contents (of the queue at the front), then creating a (constant) reference is preferred.

queue<char> const& first_line_of_construction_info = construction_info.front();
            ^^^^^^

After "read"ing, it can be poped just as in current code.

EDIT: (Thanks to @Remy Lebeau) Since copies are wastefule, myclass constructor can take construction_info by reference instead of by value.

myclass::myclass(queue<queue<char> > const& construction_info) {
                                     ^^^^^^

Look in the rest of your code too, you probably do not want multiple copies of these queue-of-queues floating around.

Aside: Unless otherwise constrained, instead of using a queue<char> for storing a line of text, consider using std::string.

6
  • I had assumed that my code was creating a copy of the front(). Now with your answer I understand how a const reference would work well. However, could you also tell me how I would go about creating a copy, so that I can see the difference.
    – kittydoor
    May 4, 2017 at 1:57
  • 1
    The myclass constructor should also be taking construction_info by reference instead of by value so that it is actually acting on the original queue and not a copy. May 4, 2017 at 2:01
  • @RemyLebeau is that because copying it is just wasteful, or are you suggesting that because of other problems?
    – kittydoor
    May 4, 2017 at 2:05
  • I'm working with char queues instead of strings because the text file format is pretty horrible, and it seemed much easier to get rid of text I read so that I wouldn't have to mess with a bunch of substrings and index calculations. I'm not sure if it is the perfect reason to use them of course, but for right now I am happy with a fifo structure.
    – kittydoor
    May 4, 2017 at 2:07
  • I see no reason to process a file's content char-by-char instead of line-by-line. It is very easy to parse lines, such as with std::istringstream. And I really don't see the need for multiple layers of queues when a single queue would suffice (and better to get rid of the queues altogether and just stick with the vector by itself) May 4, 2017 at 2:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.