std::bad_alloc

hi all,

i am using a matrix (P) (of size n^2) whose each entry is a structure (pmNODE) of size 10 bytes. so for a input of n=15000, the matrix P has size n^2 * 10 = 15000^2 *10 = 2.25 GB (approx)

pmNODE has been defined as shown below:
1
2
3
4
5
6
7
typedef struct tag_pmNode {
int xIdx;
int yIdx;
int nIntIdx;
tag_pmNode(): xIdx(-1),yIdx(-1),nIntIdx(-1) {};
tag_pmNode(int x, int y, int nInt): xIdx(x),yIdx(y),nIntIdx(nInt) {};
}pmNODE;



Initially, i was initializing the matrix P (within some function) using the command:
-----------------------------------------------------------------------------------
 
vector<vector<pmNODE> > P(n+1, vector<pmNODE> (n+1, tag_pmNode()));



and i got the following error:
-----------------------------------------------------------------------------------
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc


I assumed that this amount of memory is too much to be assigned within the stack (since P in automatic variable) and changed it to the following:
-----------------------------------------------------------------------------------
1
2
3
4
5
6
/*** allocation ***/
pmNODE **P;
P = new pmNODE*[numOfElements+1];
for (int i =0; i<= numOfElements; i++) {
P[i] = new pmNODE[numOfElements+1];
}

....
....
1
2
3
/*** deallocation ***/
for (int i =0; i<= numOfElements; i++) {
delete[] P[i];hi all,


and i again get the following error:
-----------------------------------------------------------------------------------
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc


i tried this with machines having, 2GB, 4GB and 160 GB ot RAM. But it gives the same error in all of them. so i guess the problem must be elsewhere.

the code runs absolutely fine for small inputs but crashes for large inputs > 12000 or so.

could any one please comment/help ?

thanks.


Hmm...I'm just sort of guessing, but could it be being thrown because your program is exceeding the amount of memory it was given, as opposed to how much exists?
Also, are you using a 64-bit OS? 32-bit OSes will tend to limit the process size to < 4GB (indeed they will only see the first 4GB of RAM, but certain OSes will limit the amount of accessible even further because they tend to reserve some physical address space for PCI).

EDIT: And then you'll need to compile your code to a 64-bit executable.
Last edited on
Topic archived. No new replies allowed.