Reputation: 445
I wrote the code pasted below to perform the following tasks in the order in which they are stated:
Here is my code:
#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;
}
When I execute the .cpp file I keep getting the error message:
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc Aborted (core dumped)
I have gathered this has to do with a memory shortage or variables falling out of the main() function, but I can not figure out how to address the problem in this specific situation. If it is relevant, I am working on a Linux computer.
Upvotes: 33
Views: 197296
Reputation: 31
As i was solving the "Book Shop" cses dp problem,I have written the following code..
#include<bits/stdc++.h>
using namespace std;
typedef long long in;
const int mod=1000000007;
int main(){
in n,m;
cin>>n,m;
in cost[n];
in pages[n];
for(in i=0;i<n;i++)cin>>cost[i];
for(in i=0;i<n;i++)cin>>pages[i];
vector<in>dp(m+1,0);
//cout<<dp[10]<<endl;
for(int i=0;i<n;i++){
//cout<<1<<endl;
for(int j=m;j>=1;j--){
if(j-cost[i]>=0)dp[j]=(max(dp[j],dp[j-cost[i]]+pages[i]))%mod;
}
}
cout<<dp[m]%mod<<endl;
}
AND I WAS GETTING THIS..
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Later I FIGURED IT THAT WHILE ASSIGNING n AND m INSTEAD OF cin>>n>>m;
,
I HAVE WRITTEN cin>>n,m;
.
SO,I RECCOMEND EVERYONE HAVING THIS KIND OF ISSUE ,PLEASE GO THROW YOUR CODE
ONCE,THERE IS A POSSIBILITY THAT U ARE ALSO COMMITING SAME KIND OF MISTAKE.
HOPE THIS HELPED SOMEONE.
QUESTIONMARK??
Upvotes: 2
Reputation: 27525
Loss of focus, wasted 30 mins:
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 is un-initialized, but is used below in constructor.
Since the nV took garbage value, different for each run, the program sometimes worked, and other times crashed when the nV garbage value is very high (garbage value)
Rextester didn't crash, possibly due to some different initialization, https://rextester.com/l/cpp_online_compiler_gcc
Apache Netbeans does not show this as warning
Hope that helps.
Upvotes: 6
Reputation: 7788
This code has 3 holes:
First hole: int numEntries
. Later you do: ++numEntries;
You increment unspecified value. Not sure if it's UB, but still bad.
Second and third hole:
const int length = numEntries;
int* arr = new int[length];
And
const int size = numEntries;
int matrix[size];
numEntries
has unspecified value (first hole). You use it to initialize length
and size
- that is Undefined Behaviour. But let's assume it is just some big number - you allocate memory of unspecified size (possibly just very big size), hence the std::bad_alloc
exception - it means you want to allocate more memory that you have available.
Also, matrix
is VLA
of unspecified size, which is both non-standard and Undefined behaviour.
Upvotes: 17