![]() |
|
#1
|
|||
|
|||
segmentation fault in c++hi,
i dunno why i get this segmentation fault whenever i run the codes. this program calculates a fully bracketed mathematical expression recursively. i.e. (1+(2*(2*4))) can anybody tell me how to fix the segmentation fault part plz? CPP / C++ / C Code:
Last edited by dsmith : 25-Jan-2005 at 07:34.
Reason: codes corrected
|
|||
|
#2
|
||||
|
||||
|
Hello rushman8282. Welcome to GIDForums™.
Your seg fault is coming because of the way that you split your strings and recall eval. You never null terminate your strings and then you are passing them back to eval at which point strlen looks for a null termination and doesn't find it until tromping all through unallocated memory. In addition your counting method was not quite right. Here is a replacement that is a bit closer: CPP / C++ / C Code:
You will notice that I added a couple of couts just to tell me what was going on. With this fix I got the rudimentary equation of 3+2 to return the proper value, but I think there are other more deep rooted problems with this routine. My advice is to dump a bunch of print commands into this thing to try to figure out where things are going wrong. In addition if you are using linux, I find that valgrind is an invaluable tool to track down memory leaks at exactly where they are happening. Good luck. __________________
The best damn Sports Blog period. |
|
#3
|
|||
|
|||
re: segmentation faultthank you dsmith,
i took a look and changed main algorithm. i tested out and everything works fine except the last switch part. all i need to do now is to call eval function recursively but not successful.. T.T what should i do?? CPP / C++ / C Code:
Last edited by JdS : 26-Jan-2005 at 06:19.
Reason: Please insert your example C/C++ codes between [c] and [/c] tags
|
Recent GIDBlog
Vista ?Widgets? on Windows XP by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| problem reading to a dynamic array | noamfrie | C Programming Language | 9 | 02-Jan-2005 18:35 |
| Segmentation Fault? | aaroncohn | C++ Forum | 2 | 02-Jan-2005 14:22 |
| Why seg fault in char array? | nusstu | C Programming Language | 11 | 24-Aug-2004 16:10 |
| Need a help with C code-Segmentation Fault | nkhambal | C Programming Language | 13 | 18-Jul-2004 14:58 |
| child pid xxx exit signal Segmentation fault (11) | bezak | Apache Web Server Forum | 1 | 24-Nov-2003 09:18 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The