vendredi 2 juin 2017

Error: free(): invalid next size (fast) when trying to deallocate 2d arrays

Hi so i allocated 3 2d arrays

int **distance = new int*[citynum+1];
for(int i = 1; i<citynum+1;++i){
    distance[i] = new int[money];
}
bool **visited = new bool*[citynum+1];
for(int i = 1; i<citynum+1;++i){
    visited[i] = new bool[money];
    for(int j= 0; j<money;++j){
        visited[i][j]=false;
    }
}
pair<int,int> **parent = new pair<int,int>*[citynum+1];
for(int i = 1; i<citynum+1;++i){
    parent[i] = new pair<int,int>[money];
}

And when I deallocate it

for(int i = 0; i < citynum+1; ++i){
    delete []  parent[i]; 
}
for(int i = 0; i < citynum+1; ++i){
    delete []  distance[i]; 
}
for(int i = 0; i < citynum+1; ++i){
    delete []  visited[i]; 
}
delete [] parent;
delete [] distance;

delete [] visited;

I get hit with the error

*** Error in `./foo': free(): invalid next size (fast): 0x000000000109a760 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f5dcf1447e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x7fe0a)[0x7f5dcf14ce0a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7f5dcf15098c]
./foo[0x40193e]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f5dcf0ed830]
./foo[0x400d39]

I honestly don't know why this is happening. The only thing I do between the allocation phase and t he deallocation phase is modifying the contents and reading from the the 2d arrays. I don't actually mess around with the pointers or anything. Please help thank you so much.

Aucun commentaire:

Enregistrer un commentaire