Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stack push causes critical error

Tags:

c++

stl

I am new to C++ and working with STL container at the moment. I got a serious problem executing a nodeStack.push(startnode) - the compiler shows up a

Critical error detected c0000374

Followign code shows the function where the mentioned error occurs:

vector<int> Graph::iterativeDepthSearch(map<int, vector<int>> adjlist, int startnode) {
    stack<int> nodeStack;
    vector<int> visitList;

    // Knotenbesuchsliste initialisieren
    int* val = new int(adjlist.size());
    for (int i = 0; i < (int) adjlist.size(); i++) {
        val[i] = 0;
    }

    int cnt = 1;
    nodeStack.push(startnode);
    ....
}

The error occurs in the line nodeStack.push(startnode);, startnode is initialized with 0.

like image 447
Georg Leber Avatar asked Dec 02 '22 02:12

Georg Leber


2 Answers

try int* val = new int[adjlist.size()]; you are currently allocating a single int and initializing its value, not allocating an array of ints.

The stack structure is corrupting because it is next to your pointer in the memory stack.

like image 190
totowtwo Avatar answered Dec 21 '22 12:12

totowtwo


nodeStack.push isn't really your problem. You are declaring int* val - a pointer to int, then initializing the integer at val with the size of the list. You really want int *val = new int[adjlist.size()];

like image 38
John Avatar answered Dec 21 '22 12:12

John