Reputation: 69
I'm new to C++ and Microsoft Visual Studio and I'm currently working on a lab for my data structures class, I've finished my code, but when I build and run my program it throws this error:
std::bad_alloc at memory location 0x0018C9C0.
I googled this error and I found that this error is thrown if there isn't enough memory allocated for the program or the program is trying to allocate an infinite amount of memory for something. I tried to allocate more memory in the program properties but that didn't seem to help. Looking over my code, i can't find anything that would throw this error. Here is my code for reference:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main() {
string line;
string value;
string linesArray[200];
int i = 0;
int j = 0;
int finalLine = 158;
getline(cin, line, '\n');
getline(cin, line, '\n');
/* First Line */
for (i; i < finalLine; ++i) {
getline(cin, line, '\n');
for (j; j <= 24; ++j) {
if (j = 0) {
line = line.replace(line.find(','), line.find_first_of(','), string(30 - line.find(','), ' '));
}
line = line.replace(line.find(','), line.find_first_of(','), string(20 - line.find(','), ' '));
}
linesArray[i] = value;
cout << linesArray[i] << endl;
}
return 0;
}
I tried creating a pointer to find the address that it shows in the error, but I couldn't locate it. Any help is greatly appreciated.
EDIT: Sorry that i didn't make myself clear, my input is a txt file in the format of a csv. I take in the information and on the first column of each line I place 30 spaces minus the length of the value and on the rest of the values I place 20 spaces minus the length of the value, unless there is no value, in which I place a zero with 19 spaces.
Example input:
Albania,14,29365,43301,,,,,,,13,27867,41066,,,,,,,1,1498,2235,,,
Upvotes: 0
Views: 2655
Reputation: 73444
Change this:
if (j = 0) {
to this:
if (j == 0) {
for a start. And try again...
I don't suspect that your code allocates that much memory to throw std::bad_alloc
exception.
When you do j = 0
every time at the star of your for-loop*, you create an infinite loop...
Notice that after correcting that, I get two warnings of expression result unused
, when compiling with -Wall
flag, which are not the cause of the problem but it would be nice to understand that:
for (j; j <= 24; ++j) {
doesn't need to have j
there and produce the warning, write it like this:
for (; j <= 24; ++j) {
since you don't want to initialize j
there.
Upvotes: 2
Reputation: 1772
let us consider value for line="xyz,abc"
,according to your code ,for j==0
,the code works and and the line
will have update value in which ','
is replaced,following is the out put for the 'line'
after first replace
,
xyz c
now ,for the second iteration,while replacing the variable line
,you are finding the ','
i.e line.find(',')
,which is not present in the line
string and the std::find()'
will return std::string::npos
,this is where you have to handle exceptions,
the following url tells that string::replace,you can find that ,for pos
parameter
Position of the first character to be replaced. If this is greater than the string length, it throws out_of_range.
i think you got now.
Upvotes: 1
Reputation: 56
The first two getlines are meaningless, they get overwritten by the getline in the i loop. The value string never gets assigned any value either. Then you are taking some input, which seems to be required to be of special format - because the line replace does nothing meaningful to any string that I could write. If we are to understand what you are trying to do we need to know the format of your input strings.
Insert print statements for both your loop variables i and j and the line string after any operation, like this:
/* First Line */
for (i; i < finalLine; ++i) {
getline(cin, line, '\n');
line = line.replace(line.find(','), line.find_first_of(','), string(30 - line.find(','), ' '));
cout << "[i=" << i << "] " << line << endl;
for (j; j <= 24; ++j) {
line = line.replace(line.find(','), line.find_first_of(','), string(20 - line.find(','), ' '));
cout << "[j=" << j << "] " << line << endl;
}
linesArray[i] = line;
cout << linesArray[i] << endl;
}
Also, I moved the first line replace out of the j loop.
When I ran the code the first replace produced gibberish, which might have more meaning on your specially formatted strings. Then it crashed on the first j=0 replace in the inside loop. So, in short, print the results of each operation, and you'll have a much easier time seeing what is going on.
Upvotes: 1