Segmentation fault on queue

I tried following the queue headers from /reference/queue/queue/front/ , this code gives the expected output but crashes at the end. Why is it crashing?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <queue>
using namespace std;

struct node {
    node *left;
    node *right;
    int value;
};

node *newNode(int value){
    node *Node = new node;
    Node->left = NULL;
    Node->right = NULL;
    Node->value = value;
    return Node;
}

void levelorder(node *head){
    queue<node *> x;
    x.push(head);

    while(!x.empty()){
        node *tmp = x.front();
        x.pop();
        cout<<tmp->value<<endl;
        if (tmp->left);
            x.push(tmp->left);
        if (tmp->right);
            x.push(tmp->right);
    }
}

int main(){
    node *head = newNode(1);
    head->left = newNode(2);
    head->left->left = newNode(4);
    head->left->right = newNode(5);
    head->right = newNode(3);

    levelorder(head);
    return 0;
}
Why is it crashing?

running your program through a debugger should help locate the problem(s)
In lines 27, 29 remove the semicolon

1
2
3
4
        if (tmp->left) // ; ***
            x.push(tmp->left);
        if (tmp->right) // ; ***
            x.push(tmp->right); 


The first thing to do, when there is a problem in the code is: compile the code with warnings enabled (ideally with more than one compiler), and pay attention to the warnings that are generated.

clang++
ain.cpp:27:23: warning: if statement has empty body [-Wempty-body]
        if (tmp->left);
                      ^
main.cpp:27:23: note: put the semicolon on a separate line to silence this warning
main.cpp:29:24: warning: if statement has empty body [-Wempty-body]
        if (tmp->right);
                       ^
main.cpp:29:24: note: put the semicolon on a separate line to silence this warning


g++
main.cpp: In function 'void levelorder(node*)':
main.cpp:27:23: warning: suggest braces around empty body in an 'if' statement [-Wempty-body]
         if (tmp->left);
                       ^
main.cpp:27:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
         if (tmp->left);
         ^~
main.cpp:28:13: note: ...this statement, but the latter is misleadingly indented as if it is guarded by the 'if'
             x.push(tmp->left);
             ^
main.cpp:29:24: warning: suggest braces around empty body in an 'if' statement [-Wempty-body]
         if (tmp->right);
                        ^
main.cpp:29:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
         if (tmp->right);
         ^~
main.cpp:30:13: note: ...this statement, but the latter is misleadingly indented as if it is guarded by the 'if'
             x.push(tmp->right);
             ^

http://coliru.stacked-crooked.com/a/00f1d487404da9c9

Microsoft:
source_file.cpp(27): warning C4390: ';': empty controlled statement found; is this the intent?
source_file.cpp(29): warning C4390: ';': empty controlled statement found; is this the intent?

http://rextester.com/BAGVTI2538
Topic archived. No new replies allowed.