Segmentation fault

Pages: 12
un templated and still gives the same thing... sigh haha although i appreciate you helping
Have you tried the offscreen approach (don't worry if you don't know the term, I just made it up)? Write a flow chart for your program to find out where you go wrong, it's 4 am and I don't really feel like debugging right now...
here is what we have so far
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#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.

struct node // Each node has two fields:
{
char data; // a data field,
node *next; // and a pointer field.
};


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

public:
stack();
void push(char item);
bool pop();
void search();
void view();
bool empty();
bool full();

};


stack::stack()
{
/* node<type>* temp=new node<type>;
temp-> data;
temp-> next=head;
*/
}
void stack :: push (char item)
{

node *t = new node;
node *tmp = new node;
node *trail = new node;
trail->next = NULL;
tmp->next=NULL;
t->next=NULL;

if(head == NULL) //Startig the list
{
t-> data = item;
t -> next = head;
head = t;
}
else if(head != NULL)
{
t = head;
while(t != NULL){ //Puts inside the list
trail = t;
t = 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.

bool stack :: pop ()
{

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

else
{
head = cur -> next;
delete cur;
return true;
}
}

// Accessor functions.
void stack :: search ()
{
int tmp=0;
int tmp2=0;
int tmp3=0;
int tmp4=0;

node* cur=head;
if(cur->next==NULL)
        cout<<"You do not have a problem"<<endl;
if(cur!=NULL)do
{
cur=cur -> next;

if(cur-> data == '(')
tmp++;
if(cur -> data == ')')
tmp2++;


if(cur-> data == '{')
tmp3++;
if(cur -> data == '}')
tmp4++;
}
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;
}

// Function to output the list for viewing. Assumes the
// data type is compatible with cout << .
void stack :: view ()

{
node* tmp = head;

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

}


// Is the list empty?
bool stack :: empty ()
{
if(head == NULL)
return true;
else
return false;
}

// Is the list full?

bool stack :: full ()
{
return false;
}




int main()
{


stack b;

b.push('(');
b.push('8');
b.push('*');
b.push('9');
b.push('+');
b.view();
b.search();
}
yea i have been doing that on my white board... i cant find anything because other then the new if statements that i added its the same thing as my last program which works fine.
the segmentation fault i am getting is in the search function. what should the search function look like? or does that look right? the fault i am getting is line 109
sorth, have you tried moving the cur = cur-> next right above the } while ? cause the way it is right now it will inevitably cause segmentation faults.
yes it still faults... god this is getting annoying.
you are talking about the while in the do while right? right in the search function?
mine faults right at 109 the if(cur->data == '(')....i dont think it likes that I am using the '('
So just to make sure, we both have
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if(cur!=NULL)do
{
if(cur-> data == '(')
tmp++;
if(cur -> data == ')')
tmp2++;


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

cur=cur -> next;
}
while(cur != NULL);

there? In that order?

@anderson... not you too. Char comparisons have nothing to do with segmentation faults at all. The point is that's the first time we are dereferencing cur there, so the fault is probably either in that function (which I doubt by now) or at some other point (probably in the push function, you did some really weird things there).
Last edited on
okay i got that but not it doesnt like the tmp++ or any of them
tmp doesn't reference a pointer, so obviously it doesn't crash anything.
ok so now the debugger is saying that t=t->next; is where the fault is but then when i run my other program where this code works then it works fine
Topic archived. No new replies allowed.
Pages: 12