samedi 19 janvier 2019

C++: terminate called after throwing an instance of 'std::bad_alloc' while allocating for a 1d array

levelNodes[level].push_back(stIndex);
long currsize=hi-lo+1;
long totsize=currsize*currsize; 
st[stIndex].insertedEdges = new int[currsize*currsize];    

description

when allocating memory to the array insertededges, it terminates with the said error. but when i convert the same array to a 2d array and allocate using new nested loops the same amount of memory, the memory gets allocated.

    printf("ALLOCATIONS START \n");
st[stIndex].insertedEdges = new int*[currsize];
for(int i=0;i<(currsize);i++){
    st[stIndex].insertedEdges[i]=new int[currsize];}    

How to allocate memory for 1d array?

Aucun commentaire:

Enregistrer un commentaire