![]() |
|
#1
|
|||
|
|||
Linked ListsIt seems every program that I write always ends up with segmentation faults, no matter how hard I try to prevent them. Since looking at your own code at 2:00AM is nothing like having someone else look at your code, I decided to post it up here. The description for the assignment is here http://www.cs.ucf.edu/courses/cop3502/spr2005/section2/assign3.doc
Sample input files are here http://www.cs.ucf.edu/courses/cop3502/spr2005/section2/assign.html If anything isn't apparent in the code, don't hesitate to ask. CPP / C++ / C Code:
|
|
#2
|
||||
|
||||
|
Quote:
I think that expecting other people to look at your 250+ lines of code and spot your problems isn't entirely realistic (but it could happen). I think that debugging your own code is more instructive, and is usually faster than posting a request for help and waiting for a helpful response. So, after looking at your own code until you are really, truly sick of it, here's a plan for debugging: Put printf() statements at various places in the code so that you can see what's really happening. This also gives you a chance to see if there are any places that you could improve your error checking (to prevent those dreadful segment faults). Here's a few hints to get started. First, make sure that files are successfully opened: CPP / C++ / C Code:
After scanf statements, make sure that the program is working on what you think it should be: CPP / C++ / C Code:
Now, just these two steps pointed out the following to me as I ran it with assign3_file1.txt: The first two lines of the file went through OK with the following output Quote:
Now the program hung up (ctrl-c got me out of it). So, I don't know if command = 1 is working OK or not, but I do see that command = 3 is giving me grief. So put in a few more debug statements. Let's concentrate on command 3 for now: First of all you didn't check the struct allocated at the beginning of the program. It's highly unlikely that malloc() will fail to give you the storage, but you should always check. Note that most segfaults are caused by a program attempting to access memory not allocated to it. This may be dynamic memory, where the pointer got corrupted by a program bug, or may be statically allocated memory where the program attempted to access memory outside of an array's bounds. In this case I will actually print out the value of the pointer returned by malloc(). (Your global variable "head" is set to this value; I will check it later in the program to make sure it's ok.) So your malloc() at the beginning of main() looks something like this: CPP / C++ / C Code:
Now, we want to see what's the story with command=3, so in your switch statement in main(), put in something like this: CPP / C++ / C Code:
Then, in confirm(), put in something like this; CPP / C++ / C Code:
I got an infinite loop scrolling very fast. When I hit ctrl-c, I saw a screen full of this: Quote:
So, I put in a getchar() just after the printf in the do{ then, after hitting Enter a few times the following is on the screen: Quote:
Now, that should be telling us something. You take it from here. Maybe command=1 wasn't doing its job; you can put debug statements to see how it is working (show pointers returned from malloc() if you need to see how the links are actually linking). Maybe command=1 is OK but something is wrong with command=3. Maybe both, maybe something else, maybe... Summary: segfaults are usually caused by accessing memory that doesn't belong to the program. (Also sometimes by recursive function calls that get out of control, but that doesn't apply here.) Put debug printf statements at various places in your program to see how far it gets, and what it's working with at the time. getchar() sometimes is needed to prevent the program from running away (Sometimes a program has tried to print out your debug statements and the segfault occurs before the output buffer has actually sent text to the screen, so you won't see all of the debug statements.) In any case, if the program is actually accessing illegal storage, say, due to uninitialized variables, the exact symptoms may be different with different compilers and different systems, so one gives an infinite loop, another simply hangs up, so you have to hit ctrl-c, another gives a seg fault, etc. The approach to debugging is the same: printf() is your friend. Regards, Dave We can face our problem. We can arrange such facts as we have with order and method. ---Hercule Poirot Murder on the Orient Express |
|
#3
|
|||
|
|||
|
I actually did give it a cursory lookover last night to see if something jumped out at me, but nothing did. I fully planned to debug it today, I just posted it to see if I could get a second opinion. Thanks for the pointers though.
|
|
#4
|
|||
|
|||
|
Actually, I do have a specific question now. After throwing in some debugging printf statements, I've narrowed down a problem with the insertnode function. When it finds the correct position to insert the node, it is supposed to copy the name information to the node using the strcpy function(the only way I currently know how to do this). However, it seems that for some reason, this function won't work for a pointer to a structure.
The specific lines are CPP / C++ / C Code:
Anyone have any idea why these two lines aren't valid? I don't get a compile time error, so the function is receiving the correct data types. It just seems that at run time, something is screwy. |
|
#5
|
|||
|
|||
|
argument to strcpy() are as follows
CPP / C++ / C Code:
it copies "src" string to the "dest" string. Looking at your code you need to have following CPP / C++ / C Code:
thanks, |
Recent GIDBlog
Meeting the populace by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Airport Log program using 3D linked List : problem reading from file | batrsau | C Programming Language | 11 | 29-Feb-2008 07:44 |
| [Include] Doubly-linked List | dsmith | C Programming Language | 6 | 14-Apr-2006 13:12 |
| help on linked lists any1????? | nick4 | C Programming Language | 1 | 17-May-2004 09:32 |
| linked lists, newbie needs help | moltarim | C Programming Language | 4 | 06-May-2004 11:32 |
| Linked lists | vortz83 | C++ Forum | 3 | 20-Mar-2004 06:56 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The