segmentation error

please help me i'm getting segmentation error in my program to convert and print an infix to postfix(polish) operation.
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
186
187
#include <iostream.h>
#include <conio.h>
#include<iomanip>

using namespace std;

int len;

class stack
{
	public:
	char *p;
	int top;

	void initialize(int len)
		{
		top=-1;
		p=new char[2*len];
	//	p=NULL;
		}

	void push(char a)
		{
		top++;
		p[top]=a;
		}
		
	char pop()
		{
		char temp=p[top];
		top--;
		return temp;
		}

	char view()
		{
		if(top<0)
			return NULL;
		else
			return p[top];
		}

	void traverse();

};


void stack::traverse()
	{
	cout<<p;
	cout<<endl;
	}

stack st,op;

int st_precedence(char op)
	{
	switch (op)
		{
		case '+':
		case '-':
			return 2;
			break;
		case '*':
		case '/':
			return 4;
			break;
		case '^':
			return 5;
			break;
		case NULL:
			return 0;
			break;
		default:
			return 8;
			break;
		}
	}

int ip_precedence(char op)
	{
	switch (op)
		{
		case '+':
		case '-':
			return 1;
			break;
		case '*':
		case '/':
			return 3;
			break;
		case '^':
			return 6;
			break;
		case ')':
			return 0;
			break;
		default:
			return 7;
			break;
		}
	}

int rank(char c)
	{
	switch(c)
		{
		case '+':
		case '-':
		case '*':
		case '/':
		case '^':
			return(-1);
		case '(':
		case ')':
			return 0;
		default:
			return 1;
		}
	}
	
	
int main ()
	{
	cout<<"Enter the number of varialbles"<<endl;
	cin>>len;

	//ip.initialize(len);
	char *input=new char[2*len];
	input=NULL;
	st.initialize(len);
	char *output=new char[2*len];

	cout<<"Enter the expression (Eg. a+b) Enter ) at the end)"<<endl;
	
	char *data=new char[2*len];
	cin>>data;
	input=data;
	input[2*len-1]=')';

	cout<<"Input String provided"<<endl;
	
	cout<<input<<endl<<endl;
	
	int r=0;
	
	cout<<setw(6)<<"Sr.No."<<setw(11)<<"Character"<<setw(20)<<"Stack Content"
		<<setw(20)<<"Output"<<setw(4)<<"Rank"<<endl;
		
//	cout<<"Output string : "<<endl;
	for(int i=0;i<2*len;i++)
		{
		cout<<setw(4)<<i;
		char ip=input[i];
		cout<<setw(11)<<ip;
		int ip_prec=ip_precedence(ip);
		char stack=st.view();
		int st_prec=st_precedence(stack);

		while(st_prec>ip_prec)
			{
			r=r+rank(st.view());
			if(rank>0)
				{
				op.push(st.pop());
				stack=st.view();
				st_prec=st_precedence(stack);
				}
			else
				{
				cout<<"Invalid Expression"<<endl
					<<"Press any key to terminate....."<<endl;
				getch();
				exit(0);
				}
			}

		st.push(ip);
		cout<<setw(22)<<st.p;
		cout<<setw(22)<<op.p;
		cout<<setw(6)<<r;
		cout<<endl;
		}

	op.traverse();
   getch();
	}
gdb a.out
> run
(Segmentation fault at ...)
> backtrace
Indeed. SegFaults are fantastically easy to track down.

http://www.cplusplus.com/articles/iwTbqMoL/
i'm getting the segmentation fault at 25th line but i don't understand why?
thanks in advance
i'm getting the segmentation fault at 25th line but i don't understand why?

Are you asking us if you don't understand, or are you telling us you don't understand?

p[top]=a;
If this causes a segFault, then you are trying to write to a location that is not yours to write to. Whatever the size of the array p is, top is too big. What's the value of top? What size is the array p?
Last edited on
I'm telling that i don't understand?
In the initialize function i already specified top=-1 and in the push function i first incremented top(top++) and then accessed p[top]. please help me with the problem.
At the point in the code where the segFault happens, what's the value of top and what size is the array p? Don't guess. Check. Either use a debugger, or put some code in to tell you.
but how do i check i running a windows system wherein i don't get a dedicated debugger which shows me value and size of array. i use dev-cpp on win 7. can u help me by checking it on your machine please. or any solution using dev-cpp. and i guess the array is of suitable length. help lease. thanks in advance.
Here's an example of a popular way to determine the value of a variable at a given point.
cout << "The value of top right now is " << top << endl;

and i guess the array is of suitable length

That's impossible. That's the whole point. If the segault is on the line you said, you're trying to write outside the array. If the array was of a length suitable to where you're trying to write, everything would be fine.
Last edited on
If you have to write "help please " in every post ankit, you're doing something really wrong.
Why aren't you printing in every step or using gdb ?

use std::cout << after every step to see if your code is behaving the way you want.
better yet use gdb but i am guessing you don't want to take that pain.

It's as if you want others to find the problem for you and fix it for you ? Are you just trying to finish up a code and submit or to actually learn ??

~navderm
hello navderm i actually want to learn but i'm very new to pointers and frequently get segmentation faults but those were generally due to accessing null pointers. but this the pointer isn't set to null and i also tries cout statements in the initialize(line 15) as well as push(line 25) code and the top is correctly set to 0 for the first element, and also the size is set 2*len appropriately. I'll try to check it out on gdb but currently not having any linux system with me. Thanks for help @navderm.
1
2
3
4
5
6
7
8
	char *input=new char[2*len];
	input=NULL; //leak
//...
	input=data; //make up your mind
	input[2*len-1]=')';

	cout<<"Input String provided"<<endl;
	cout<<input<<endl<<endl; //Not '\0' at the end 


global, global everywhere.
You didn't `initialize' stack op. That should be the work of a constructor
Very very thanks ne555 i sorry for doing such mistakes actually i'm newbie and just learning cpp and pointers is my fourth chapter. I'll try reducing globals and optimize the program.
Thanks again for guiding me.
Topic archived. No new replies allowed.