Unpredictable output of c code.debugger isnt helping
Hello friends, I have written a c code for performing expression conversion:
1.Infix to postfix 2.infix to prefix 3.Postfix to prefix. Also to evaluate a given infix expression But each time I execute it in the terminal, I've got different output which are totally incorrect.. Some of those are segmentation fault,etc....sometimes it does not execute beyond taking the input(forever takes input...does not proceed to the next line...)...sometimes it gives the output partially correct which includes some of the input strings..I am unable to execute any of the conversions correctly...Debugger isnt helping much... Could you help me to find the bug???Please? Here is my code: Code:
/*Aim:This program performs expression:1.Infix to postfix Thank you. |
My advice:
1. Never use scanf(). It's doesn't check if the input makes sense (applies to %s too, but especially important with %d and such). You don't know if it will fit the buffer. Out of buffer == CRASH. Also: don't use gets(). Instead: for instance fgets (notice difference). 2. You don't check if the expression makes sense (probably none of the mine managed that...). If the number of ( and ) doesn't mach, for instance, you'll be searching for the missing character. Notice: Code:
while((tkn=in[i])!='\0') 3. Always initialize variables/tables. You initialize variables, but not tables. Memset them to zeros before use (in your case: in the main while loop). 4. Please give some input data that should work (and the results) - I think I know what you're trying to archive, but I'm not that sure. 5. Download/install valgrind. Read the manual and start using it. It's good to do it from the very beginning - it will help you in many frustrating situations. Also look into gdb. Valgrind is a tool that shows (in short) bad memory accesses. Like index out of table and such. Gdb is a debugger. When it comes to your program, valgrind says you're running out of table in in_post, for instance (places differ depending on the input data). |
it's difficult to debug such a large program. A little work on your part to narrow down the problem would help.
I haven't found all the bugs, but the cause of at least one of your segfaults is that you're not checking to ensure your indexes stay within the bounds of your char arrays. You have MAX defined (that's a good thing) and you're using MAX when you create your char arrays, however, you never check if you've gone past MAX when you're iterating through the arrays. I specifically ran into this on in_post where you do a ton of j++'s, but you never check j to see if is larger than MAX. I used a debugger to find this, but a simple "printf("j: %d\n",j);" would do it also. I see j's of 12000 before it cores, so it's going way out of bounds. Not to sound kurt, but you've posted code with essentially no description of the problem (other than, "it behaves weird"), no example of input values, no example of what output shoud look like, and none of the work you've done to try to debug it yourself. Not only that, but it's over 260 lines of code... any of which could contain a problem or error... a little help from you would make it easier for others to debug. If you're brand new to c/c++ & honestly have no clue what is wrong - say so, but leaving it this open is frustrating (at least to me). On a positive note, go ahead and check j before you try to use it as an index & see if that helps. If not, post back your revised code & hopefully an idea of where the problem might be. along with an example of input & what the output should look like. ;) |
A tool could help you to figure out where the problem is: valgrind and its memcheck option. It could be hard to use, so I recommand you "alleyoop" which is a easy to use valgrind gui...
|
At all,
Your replies were enlightening(bound checks)...But I couldnt figure the out of bounds condition myself(I was unable to use valgrind.:( ) I am still getting incorrect output. (see 1) for those) As for the inputs and the required outputs: The inputs should be correct logically as well as correctly paranthesised(in case of the infixexpression). here is the expected ip and corresponding o/p... 1) Infix expression: (A+B)*(C+D) Prefix expression: *+AB-CD //instead I get +(A*B-CD :( Postfix expression: AB+CD-* for the evaluation part: if the values of A,B,C and D are taken as 10,5,12 and -2 respectively, the output should be 150. 2) Infix expression: A/B*C-D+E/F/(G+H) Prefix Expression: +-*/ABCD//EF+GH (is it correct?I have converted manually) Postfix: AB/C*D-EF/GH+/+ @Briank I couldnt use the debugger in linux(gdb as well as valgrind) ....So i used Turbo C,it's giving me just two warnings... that functions precedence and ecaluate should return values...(why??? as you can see they are retuning values!) also why is j going to 12000s amd all that??I havce initialised it to 0/-1...and the loops are not being executed those many times.. Your suggestion was good.thanx:) @Mara yeah,fgets is better..thanx but I am still using scanf just for this code for the sake of finding the bug in the original code..scanf is not that bad... @ All Is this a problem of the compiler???? or am I wrong???(I am still unable to find a bug in this code)...Really unhappy now. :( |
Quote:
Quote:
Quote:
|
With the examples I have found at least two problems:
Code:
void in_post(char in[MAX],char post[MAX]) Code:
while((tkn=in[i++])!='\0') Second thing is in in_pre: Code:
else if(tkn==')') |
Hi friends,
I have ultimately fixed the program(I had some lousy goofups in functions post_pre and in_post->pointed out by Mara).I wrote a new code for some functions.... @jf.argentino...thanx for the suggestion to use ddd...(It's excellent.) Also I found this particular advice as an eye-opener... Quote:
@Mara Thanx for the reply. It was really helpful... Here is my new code.... Code:
/*Aim:This program performs expression:1.Infix to postfix |
All times are GMT -5. The time now is 08:05 PM. |