1

I made a Node class and tried to implement a simple tree structure using C++. I tested the code, visual studio was able to compile and output the expected results. However, when I tried Dr Memory to check memory leak, Dr Memory always crash at certain points, including: 1) in the Node class constructor "col = vec[0].size()" 2) in the distance function "col = vec[0].size()"

If I kept those lines, visual studio can compile and output the right thing, however, Dr Memory crash... If I commented those lines, Dr Memory works... So I am quite confused what part went wrong, is the nodes on the heap? or the vector? or Dr Memory... Thank you

// node class
class Node {
public:
    vector<vector<int>> board;
    int row;
    int col;
    Node(vector<vector<int>> &vec): board(vec) {
        row = vec.size();
        col = vec[0].size();
    }
    Node(Node *&node): board(node->board) {}
};

// calculate distance
int distance(v2_i &vec) {
    int ans = 0;
    int row = vec.size();
    int col = vec[0].size();
    for(int i=0; i<row; i++) {
        for(int j=0; j<col; j++) ans += abs(vec[i][j]-(i*row+j+1));
    }
    return ans;
}

// main function
int main() {
    vector<vector<int>> startBoard = input_txt();
    Node *node_1 = new Node(startBoard);
    Node *node_2 = new Node(node_1);

    cout << distance(node_2->board) << endl;

    delete node_1;
    delete node_2;
    return EXIT_SUCCESS;
}
Vince
  • 11
  • 1
  • 1
    Are you sure `input_txt()` added items to the vector? `col = vec[0].size();` will be UB if the vector is empty. You probably want to replace `col = vec[0].size();` with `if (row > 0) col = vec[0].size(); else col = 0;` – drescherjm Sep 14 '17 at 17:49
  • Thanks for your reply, yes I am sure input_txt() return the correct 2d vector. I did not post that function in the original question – Vince Sep 14 '17 at 18:10
  • So the "vec" is a 2d vector, thus the visual studio can compile and I tried to print this 2d vector, the result is correct. However, I am not sure why Dr Memory didn't work... Is that because I created nodes on heap...? – Vince Sep 14 '17 at 18:13
  • I would still correct for this potential bug. In `Node::Node(vector> &vec)` and `int distance(v2_i &vec)`. – drescherjm Sep 14 '17 at 18:13
  • I believe that is all we can help for the code presented. – drescherjm Sep 14 '17 at 18:14
  • Thanks, I will fix that bug you mentioned. I am still quite confused why everything was fine in visual studio, however, Dr Memory crashed... – Vince Sep 14 '17 at 18:18
  • After you fixed the bug (in every place) does `Dr Memory` still crash? – drescherjm Sep 14 '17 at 18:22
  • Actually based on your suggestion, I found out an interesting fact.... I tried to output the vec.size(), from visual studio, vec.size() = 4, however, from DrMemory, vec.size() = 0... – Vince Sep 14 '17 at 18:27
  • So when you use `DrMemory` your vector is empty. – drescherjm Sep 14 '17 at 18:28
  • So yes, I really appreciate your advice, I should add some if statement to check the vec.size(), however, now the problem is the result from visual studio and DrMemory is different... I guess the problem is still the dynamic memory? since I made all nodes on heap – Vince Sep 14 '17 at 18:28
  • ***I guess the problem is still the dynamic memory?*** I say no. `DrMemory` just exposes the potential bug (blindly assuming the vector is not empty) that you have in your code. I believe it is a good thing that this type of bug is caught by DrMemory. – drescherjm Sep 14 '17 at 18:30
  • You are right... So the problem should be the output of input_txt()... I tried to change the project working directory back to project default, however it still didn't work... – Vince Sep 14 '17 at 18:37
  • 1
    The problem was the working directory. When I modified the working directory of Dr Memory to the same directory as VS, it worked. Thanks – Vince Jun 09 '19 at 23:07
  • I am surprised you replied after so long. Glad you got this working.. – drescherjm Jun 09 '19 at 23:11
  • Thanks for your help!! And I am also surprised that you replied so quickly after so long, haha – Vince Jun 09 '19 at 23:42

0 Answers0