Seg fault but why.

i had this code in my last program and it worked fine and it works on my friends mac but it wont work on my g++ compiler or devc it gives a seg fault at the t=t->next; how can i fix this?
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
template<class type>
void stack<type> :: push (type item)
{
node<type> *t=new node<type>;
node<type> *trail = new node<type>;
node<type> *tmp = new node<type>;
trail = NULL;

if(head == NULL)
{
t->data = item;
t->next = head;
head = t;
}
else if(head != NULL)
{
t = head;
while(t != NULL)
{
trail = t;
t = t->next;
}
tmp->data = item;
tmp->next = t;
trail->next = tmp;
}
}

Last edited on
line 21 is the problem
line 21 isn't the problem. It crashes when you don't initialize 'head' in your constructor.

You don't need to create three nodes for one item. And it's not the way a stack works. A stack pushes the item on the top and pops it from there.

I wrote an example somewhere but since I don't think it's appreciated...
write but this is a queue and the head is initialized, this is not the entire program just the one function
sorthon, now I figured out what was wrong with your code. You do not initialize the head (at least you didn't in the code you gave me last time). if(head == NULL) - last time you didn't initialize head to NULL in your constructor, and I bet $10 you still don't.

And again sorthon, I have told you like 5 times already- segmentation faults are not caused by the line they appear in. Segmentation faults are caused by accessing illegal memory regions, which usually happens due to incorrect handling of pointers.

Also, I told you last time that your push code creates memory leaks, it appears you still didn't fix that.

Aside from that, you don't really create stack functionality there.
Then why do you call it stack when it's a queue?

If you don't initialze 'next' (null) it will crash as well.

Look at line 5 and 7. Does it make sense to you?
head is declared and initialized and by nature is defaulted to NULL or at least that is what my professors tell me.
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <iomanip>
#include <iostream>
#include <typeinfo>
#include <cstdlib>

using namespace std;

// Specification file for a linked list abstract data type class.
// This linked list is actually a stack, since all additions and
// removals are at the head.
template<class type>
struct node // Each node has two fields:
{
type data; // a data field,
node<type> *next; // and a pointer field.
};

template<class type>
class stack
{
private:
node<type> *head; // Pointer to the first cell.

public:
stack();
void push(type item);
bool pop();
void search();
void view();

};

template<class type>
stack<type>::stack()
{

}

template<class type>
void stack<type> :: push (type item)
{
node<type> *t=new node<type>;
node<type> *trail = new node<type>;
node<type> *tmp = new node<type>;
trail = NULL;
t==NULL;
if(head == NULL)
{
t->data = item;
t->next = head;
head = t;
}
else if(head != NULL)
{
t->next = head;
while(t != NULL)
{
trail = t;

t->next = t->next;
}
tmp->data = item;
tmp->next = t;
trail->next = tmp;
}
}


// Function to remove the first element from the stack and
// return it to the caller.
template<class type>
bool stack<type> :: pop ()
{

node<type>* cur;
cur = head;
if(cur == NULL)
{
return false;
}

else
{
head = cur -> next;
delete cur;
return true;
}
}
// Accessor functions.
// Function to see whether an element is on the list.
template<class type>
void stack<type> :: search ()
{
type tmp;
type tmp2;
type tmp3;
type tmp4;

node<type>* cur=head;
if(cur!=NULL)do
{
if(cur-> data == '(')
tmp = cur->data;
if(cur -> data == ')')
tmp2= cur->data;

if(cur-> data == '{')
tmp3 = cur->data;
if(cur -> data == '}')
tmp4 = cur->data;

cur=cur -> next;
}
while(cur != NULL);
if (tmp == tmp2)
cout << "The operation has the correct amount of parenthesis." << endl;

else
cout << "You do not have the appropriate number of parenthesis." << endl;

if (tmp3 == tmp4)
cout << "The operation has the correct amount of braces." << endl;

else
cout << "You do not have the appropriate number of braces." << endl;

if(tmp==0 && tmp2==0 && tmp3==0 && tmp4==0)
cout<<"There is proper grouping"<<endl;
}


template<class type>
void stack<type> :: view ()
{
node<type>* tmp = head;

while(tmp != NULL){
cout << tmp -> data;
tmp = tmp -> next;
}

}

int main()
{


stack<int> b;

//b.push('(');
b.push(8);
//b.push('*');
b.push(9);
b.view();
cout << endl;
//b.search();
} 
off to the class this is due in haha just gana turn it in late been really sick and not in the mood to think i just dot get why it worked before on a mac but not on my computer.
i get and understand that there are leeks, but that doesn't explain why im looking at it working on a mac but the exact same thing will not work on my pc. even more confusing is that they use the same compiler.
1
2
3
4
5
template<class type>
stack<type>::stack()
{
     head = NULL;
}


Done. The value of head is undefined if you don't do this- and with undefined things, even the same compilers may behave differently with different implementations. It may be set to NULL automatically, or it's just left as it is (which might be any random value that was in the memory previously at that point).
wow... my teacher is an idiot, i asked him this several times and he assured me that its defaulted to NULL alright well that did in fact fix it, thank you very much for sticking in there with me and helping sorry if i at any point come over as rude or snappy.
but it was a good learning experience :)
Your teacher is probably not an idiot, but many teachers really lack programming experience.
Topic archived. No new replies allowed.