std::bad_alloc

closed account (DEUX92yv)
Every sixth or seventh time I try to run the program it works, but otherwise I get this:
1
2
3
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted


And here's the relevant parts of my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// includes

// prototypes

int main(int argc, char *argv[])
{
  int rows, cols, result;
  char *mymaze;

  if(argc < 3) {
    cerr << "Usage: ./maze in_filename out_filename" << endl;
    return 1;
  }

  mymaze = read_maze(argv[1], &rows, &cols); // this is where it makes it to

  delete [] mymaze;

  return 0;
}

char * read_maze(char *filename, int *rows, int *cols )
{
	char *mymaze = new char[*rows * *cols]; // what's wrong here?
	ifstream mazein;
	mazein.open(filename);
	mazein >> *rows >> *cols;
	for (int i = 0; i < (*rows * *cols); i++) {
		mazein >> mymaze[i];
	}
	mazein.close();
	return mymaze;
}


Why does this happen? And why would it happen only every so many-th time?
Last edited on
This exception is thrown by new operator when it is failing to allocate memory. Use nothrow construct and check the pointer if it is null to get rid of the exception.

http://www.cplusplus.com/reference/std/new/operator%20new%5B%5D/
closed account (DEUX92yv)
I don't really understand that literature. Is the nothrow a kind of suppressing the bad_alloc? If so I'm not supposed to do that, and I'd like to learn how to dynamically allocate without having such an issue. It shouldn't be a memory problem, because the arrays I'm using are only 25 characters. Even so, if running out of memory is the problem, if someone tried my program on a computer with more available, the problem wouldn't arise, would it?
Last edited on
It looks like you don't set a value for rows or cols, so they will be whatever garbage value is in memory - garbage values that mean you're asking for a huge amount of memory.
closed account (DEUX92yv)
They're set by reading in the text file. rows and cols are the first two items in the file
They're set by reading in the text file. rows and cols are the first two items in the file


Which is done after using them to allocate memory. If you want to use rows and cols to calculate the amount of memory to allocate, you have to set their values before you use them to calculate the amount of memory.
Last edited on
closed account (DEUX92yv)
Thank you so much. That completely slipped my mind. But now I'm getting a segmentation fault:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char * read_maze(char *filename, int *rows, int *cols )
{
	ifstream mazein (filename);
	if (! mazein.is_open()){
		cout << "Failed to open " << filename << endl;
		return NULL;
	}
	mazein >> *rows >> *cols;	// Get dimensions
	char * mymaze = new char[*rows * *cols];	// Allocate array
	for (int i = 0; i <= ((*rows * *cols) + *rows); i++) {
		if (mazein.good()) {
			mymaze[i] = mazein.get();
		}
	}
	mazein.close();
	return mymaze;
}
Last edited on
Lets say rows is 5 and cols is 5. This means you allocate enough space for 25 char objects.

In your loop, how many do you then try to write?
 
(int i = 0; i <= ((*rows * *cols) + *rows); i++)


From i=0 to i= (5 * 5) + 5, which is 30. So you're trying to write past the end of the array.

Here is how to find segFaults for yourself: http://www.cplusplus.com/articles/iwTbqMoL/
Last edited on
closed account (DEUX92yv)
Much better now. Thanks for your help.
Topic archived. No new replies allowed.