PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, October 30, 2022

[FIXED] Why am I getting an std::bad_alloc error

 October 30, 2022     c++, eof, fstream     No comments   

Issue

I'm having an issue when running the code below. Every time I set the while loop to reach the .eof() it returns a std::bad_alloc

inFile.open(fileName, std::ios::in | std::ios::binary);

        if (inFile.is_open())
        {
            while (!inFile.eof())
            {
                read(inFile, readIn);
                vecMenu.push_back(readIn);
                menu.push_back(readIn);
                //count++;
            }

            std::cout << "File was loaded succesfully..." << std::endl;

            inFile.close();
        }

It runs fine if I set a predetermined number of iterations, but fails when I use the EOF funtion. Here's the code for the read function:

void read(std::fstream& file, std::string& str)
{
    if (file.is_open())
    {
        unsigned len;
        char *buf = nullptr;

        file.read(reinterpret_cast<char *>(&len), sizeof(unsigned));

        buf = new char[len + 1];

        file.read(buf, len);

        buf[len] = '\0';

        str = buf;

        std::cout << "Test: " << str << std::endl;

        delete[] buf;
    }
    else
    {
        std::cout << "File was not accessible" << std::endl;
    }
}

Any help you can provide is greatly appreciated. NOTE: I failed to mention that vecMenu is of type std::vector and menu is of type std::list


Solution

The main problems I see are:

  1. You are using while (!inFile.eof()) to end the loop. See Why is iostream::eof inside a loop condition considered wrong?.

  2. You are not checking whether calls to ifstream::read succeeded before using the variables that were read into.

I suggest:

  1. Changing your version of read to return a reference to ifstream. It should return the ifstream it takes as input. That makes it possible to use the call to read in the conditional of a loop.

  2. Checking whether calls to ifstream::read succeed before using them.

  3. Putting the call to read in the conditional of the while statement.

std::ifstream& read(std::fstream& file, std::string& str)
{
   if (file.is_open())
   {
      unsigned len;
      char *buf = nullptr;

      if !(file.read(reinterpret_cast<char *>(&len), sizeof(unsigned)))
      {
         return file;
      }

      buf = new char[len + 1];

      if ( !file.read(buf, len) )
      {
         delete [] buf;
         return file;
      }

      buf[len] = '\0';

      str = buf;

      std::cout << "Test: " << str << std::endl;

      delete[] buf;
   }
   else
   {
      std::cout << "File was not accessible" << std::endl;
   }

   return file;
}

and

inFile.open(fileName, std::ios::in | std::ios::binary);

if (inFile.is_open())
{
   std::cout << "File was loaded succesfully..." << std::endl;

   while (read(inFile, readIn))
   {
      vecMenu.push_back(readIn);
      menu.push_back(readIn);
      //count++;
   }

   inFile.close();
}


Answered By - R Sahu
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing