How To Solve The "Unhandled exception... std::bad_alloc" Problem?

Hello Professionals,

Good day. It is known that dynamic arrays are powerful than static arrays considering the fact that dynamic arrays can handle huge number of arrays unlike in static. However, when I was trying to run my program, it says:

"Unhandled exception at 0x75A7CBB2 in Turk's Method 3D.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x00EFF6E4. occurred"


I don't know where is the part of the problem. I have tried implementing it to all variables in dynamic arrays, but it's kinda like this error is showing on this part?

Image: https://i.imgur.com/mVCjEMi.png

I also did the "try and catch" pattern as mentioned in this link http://en.cppreference.com/w/cpp/memory/new/bad_alloc
but apparently, it didn't work.

This is the sample code of implementing "try and catch" to my program...

1
2
3
4
5
6
7
8
	try {
		for (int m = 0; m < 100000000; m++) { 
			aLU[m] = new float[100000000];
		}
	}
	catch (const std::bad_alloc& e) {
		std::cout << "Allocation failed: " << e.what() << '\n';
	}


What should I do next to resolve this issue?
Last edited on
Another update:

I tried to decrease the number of arrays from "100000000" to "10000000" and this is what I saw in the screen...

Image: https://i.imgur.com/gfXopbz.png

Another update:

But when I tried to lower down the value to "100000", it shows like this:

Image: https://i.imgur.com/dp6bIfY.png



1
2
3
4
for (int m = 0; m < 100000000; m++) 
{ 
  aLU[m] = new float[100000000];
}

How much is 100000000 * 100000000 * sizeof(float) ?
How much memory does your PC have ?
Last edited on
Hello @Thomas1965!
Hmm, is this what you are asking about?

Image: https://i.imgur.com/h10SnoT.png
Normally you get a bad_alloc exception when there is not enough memory for the operator new.
So yes my suspect was that you don't have enough memory for creating so many huge arrays.

If you tell us what you actually want to do someone may find a solution that doesn't involve keeping everything in memory all the time.
People were hoping you would work this out for yourself.

A float is typically 4 (or 8) bytes. Let's assume 4.

So 100000000 floats is 400000000 bytes. About 400 Megabytes.

You're trying to create 100000000 arrays of this size, so you're trying to allocate 100000000 * 400 Megabytes, which is about 40000000 Gigabytes.

See if you can find out how much memory your PC has, and see if you can spot a problem with trying to use 40000000 Gigabytes of memory.
I'm just wondering if this can be solved by using linear arrays or std::vector? But these methods themselves are such new topics for me to absorb..
Your matrix is almost certainly sparse; use a sparse matrix representation. For instance,
https://www.boost.org/doc/libs/1_67_0/libs/numeric/ublas/doc/matrix_sparse.html#compressed_matrix

1
2
3
4
5
6
7
8
9
10
11
12
#include <boost/numeric/ublas/matrix_sparse.hpp>
#include <iostream>

int main () {

    const std::size_t N = 100'000'000 ;
    boost::numeric::ublas::compressed_matrix<double> mtx( N, N );

    for( std::size_t i = 0 ; i < N ; i += 1000 ) mtx(i,i) = 1.0 ;

    std::cout << std::fixed << mtx( 50'000'000, 50'000'000 ) << '\n' ; // 1.000000
}

http://coliru.stacked-crooked.com/a/74baeef75a90ab89
Thank you for the response @JLBorges. I am wondering if this "boost/numeric/ublas/matrix_sparse.hpp" library is the best option for handling "bigger" matrices as compared to std::vector and linear arrays?

Your matrix is almost certainly sparse; use a sparse matrix representation.


Hmm, do you mean "dense"? I am not good in terminologies, I just heard this word from my classmates, but I am not sure if it's correct as well.
> I am wondering if this "boost/numeric/ublas/matrix_sparse.hpp" library is the best option
> for handling "bigger" matrices as compared to std::vector and linear arrays?

Eigen is the other option that I know of. https://eigen.tuxfamily.org/index.php?title=Main_Page

I can't say if either of these two provide the best option; I've very little experience in this domain. I suspect that what is the best option may depend on the specific problem that you are trying to solve.


> do you mean "dense"?

See this wiki page: https://en.wikipedia.org/wiki/Sparse_matrix
Topic archived. No new replies allowed.