• Register
0 votes
887 views

Problem :

I have written the code to perform the following tasks in the order in which they are stated:

1.    Read the input file and count the number of entries in it

2.    Create the array of appropriate size (size equal to number of entries)

3.    Go back to the starting of the input file and read it again

4.    Store the entries in the array

5.    Print out number of entries in your file and the entries themselves.

 

But when I try to execute the .cpp file I keep facing the following error message:

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

I have tried to gather information and came to conclusion that this has to do with a memory shortage or the variables falling out of the main() method, but I am unable to figure out how to solve problem in this specific situation. Please Note: I am working on a Linux computer.

6 5 3
7,540 points

Please log in or register to answer this question.

2 Answers

0 votes

Solution :

This code must have 3 mistakes:

First mistake: int numEntries. Later you do: ++numEntries;

You are incrementing the unspecified value. Not sure if it's the root cause, but still unacceptable.

Second and third mistake:

const int length = numEntries;

int* arr = new int[length];

And

const int size = numEntries;

int matrix[size];

Your numEntries has the unspecified value (first mistake). You are using it to initialize the length and size now this is Undefined Behaviour. Let us consider it is just some big number - you are allocating memory of unspecified size, So the std::bad_alloc exception - it means you are trying to allocate more memory than you have available.

Also the matrix is a VLA of unspecified size, which is both the non-standard and Undefined behaviour.

9 7 4
38,600 points
0 votes

Solution:

Sometimes we face this error while coding. Today we will learn why this error shows and how to solve this problem.

This error indicates that you are running out of memory. It has typically been seen in cases where the hypothesis used for the search contains a large number of sites or a large number of identical sites. Using partial matching for large hypotheses may also contribute to the error: for example, asking that the search must match 6 out of 17 sites. The workaround is to reduce the number of sites in the hypothesis.

The problem shows in your code when you store the memory address of a local variable (local to a function, for example) in a global variable. You can't store the memory address of a local variable in a global variable

Example:

RectInvoice rect(vect,im,x, y, w ,h);
this->rectInvoiceVector.push_back(&rect);

There, &rect is a temporary address (stored in the function's activation registry) and will be destroyed when that function end.

The code should create a dynamic variable:

RectInvoice *rect =  new RectInvoice(vect,im,x, y, w ,h);
this->rectInvoiceVector.push_back(rect);

There you are using a heap address that will not be destroyed in the end of the function's execution.

10 6 4
31,120 points

Related questions

1 vote
1 answer 2.6K views
2.6K views
Problem : My stoi function is crashing the program with the error message as below : "****@****:~> g++ -std=c++0x m1.cpp stimulation.h stims.h Task.h exoskeleton.h ARAIG_Sensors.h Profile.h ARAIG_Sensors.h:1:9: warning: #pragma once in main file [ ... know why is there the instance being passed to the method but it is still returning the above error. Can somebody help me in resolving the issue?
asked Dec 12, 2019 alecxe 7.5k points
1 vote
1 answer 262 views
262 views
Problem : I am a Computer Science Student. I am trying to learn the C++ programming. I want to execute a very simple averaging code written by me but I always face below error: Terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: ... larger than 0? So I am confused because of above error. Can some expert in C++ guide me in fixing above error for me.
asked May 14, 2020 Martin K 6.6k points
1 vote
1 answer 1.3K views
1.3K views
Problem : I am literally trying to start learning C++, and I have written very simple code in C++ as follows for your reference: string mySong(string songlist[], int songnum) { for (i = 0; i < songnum; i++) { output += songlist[i]; output += bone1; ... available answers make sense with what I know about programming so far. So I am looking for the pin point solution to my above C++ related error.
asked May 13, 2020 Martin K 6.6k points
0 votes
1 answer 2 views
2 views
terminate called after throwing an instance of std logic_error
asked Feb 16 ArifulIslam 7.5k points
0 votes
1 answer 103 views
103 views
Problems: What will be the value of x after the following code is executed? int x, y = 4, z = 6; x = (y++) * (++z); A) 24 B) 28 C) 30 D) 35 I am new to programming , Can anyone help me ?
asked Feb 13, 2020 maddi86 5.4k points
0 votes
1 answer 12 views
12 views
When we are trying to update client detail and got this error.
asked Sep 28, 2020 Daniel Anderson 4k points
0 votes
1 answer 10 views
10 views
let's imagine we have two classes A and B: from class A in private I create a vector of pointers to instances of B vector <B *> punt; How would you add pointers to instances of B in that vector? How would those pointers be removed from the vector? Like a normal ... (name); // assuming you have a constructor that assigns the name     punt.push_back (newB); ??? Thank you very much and greetings !
asked Aug 14, 2020 game 4.6k points
0 votes
1 answer 25 views
25 views
I want to know the reason why
asked Jul 25, 2020 James Martin 2.2k points
1 vote
1 answer 26 views
26 views
Problem: I am a new student of c++ programming. I am taking an online-based course to learn it. I&rsquo;ve written a pretty simple program that adds two numbers through a function. I am attaching my code snippet below, please have a look: int promptNumber() { int n; std: ... ;t run the program. I keep getting an error: cout is not a member of std Could any of you please shed some light on this?
asked Mar 31, 2020 Gavin 15.3k points
0 votes
1 answer 1.9K views
1.9K views
Problem : I want to convert the string into an int by using stoi, however for some unknown reason it is giving me an error and giving me following message: "libc++abi.dylib: terminating with uncaught exception of type std::invalid_argument: stoi: no conversion" The myline which is ... ; split >> mybeginning_time; split >> myminutes; **int time = stoi(mytemp);** time = time > 0 && time < 2400;
asked Dec 5, 2019 alecxe 7.5k points