Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Error: terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc – quick tips
Next
Answered
Felix Martin
  • 23
Felix Martin
Asked: May 18, 20222022-05-18T15:36:06+00:00 2022-05-18T15:36:06+00:00In: cpp

Error: terminate called after throwing an instance of ‘std::bad_alloc’ what(): std::bad_alloc – quick tips

  • 23

. Advertisement .

..3..

. Advertisement .

..4..

I encountered the following problem in completing my work:

terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted (core dumped)

Below is the code I ran:

#include <iostream>
 #include <fstream>
 #include <exception>
 
 using namespace std;
 
 int main(int argc, char* argv[]){
 
  ifstream inFile(argv[1]); //passing arguments to the main function
  int numEntries;
 
  if(!inFile){
  cout << "file not found" << endl;
  return 1;
  }
 
  string entry;
  while (!inFile.eof()){ //counting the number of entries
  getline(inFile,entry);
  ++numEntries;
  }
 
  const int length = numEntries; //making an array of appropriate length
  int*arr = new int[length];
 
  inFile.clear(); //going back to the beginning of the file
  inFile.seekg(0, ios::beg);
 
  int i = 0;
  const int size = numEntries; //making an array to store the entries in the file
  int matrix[size];
  int pos = 0;
 
  int variable = 0;
  while(pos < size){
  inFile >> variable;
  matrix[pos] = variable;
  ++pos;
  }
  cout<< numEntries << "entries have been read"<< endl; 
  inFile.close();
  for(int i = 0; i < pos; ++i)
  cout << matrix[i] << endl; //printing out the entries
  return 0;
 }

What’s causing it, and how can it be resolved in the “terminate called after throwing an instance of ‘std::bad_alloc’ what(): std::bad_alloc“ in the cpp?

terminate call
  • 2 2 Answers
  • 170 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    lyytutoria Expert
    2022-06-29T00:23:27+00:00Added an answer on June 29, 2022 at 12:23 am

    The cause:

    You have got this error because you are having 3 holes:

    1st hole is  int numEntries. Then you do: ++numEntries;

    You are increasing the undefined values. Even if it is UB, it’s still faulty.

    2nd hole is:

    const int length = numEntries;
    int* arr = new int[length];

    3rd hole is:

    const int size = numEntries;
    int matrix[size];

    numEntries (first hole) has undetermined values. It is Specified Behavior when you use it to initialize length and size. The std::bad alloc error indicates that you are attempting to allocate more memories than is currently available. However, let’s assume that it is just a large number and that you are allocating memory of an indeterminate amount (perhaps just a very large value).

    Additionally, the behavior of the matrix, which is undefined and non-standard, is a VLA of undefined size.

    These are the reasons of your error.

    Solution:

    You have to provide the specified values for numEntries, matrix and give enough memories for std::bad alloc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Arthur Courtois
    2022-05-25T20:24:10+00:00Added an answer on May 25, 2022 at 8:24 pm

    Focus loss, 30 minutes wasted

    class Cls1{
     int nV; // <------------- nV is un-initialized
     vector<bool> v1;
    public:
     Cls1 () {
     v1 = vector<bool> (nV + 1, false); // <------------------ nV is used
     }
    };

    As you can see, nV has not been initialized but is used in the constructor.

    The nV takes garbage value for each run. This means that the program can sometimes work and crash when the garbage value (or garbage value) is too high.

    • Rextester didn’t crash, possibly due to some different initialization, https://rextester.com/l/cpp_online_compiler_gcc

    • This warning is not displayed by Apache Netbeans

      • Git files allow you to see the changes and will locate these issues.

    We hope that this helps.

    • 21
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.