Thread: [Dev-C++] Linked List...Again
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
From: Jordan B. <jor...@me...> - 2011-05-09 15:45:12
|
Hey Everyone and good morning, I am in the process of creating a to do list console application and seems I have hit a wall so to speak. Below I have an exert from my actualy class. I have been trying to get this linked list working and so far it has failed. I did not know if it was the driver program that was failing, the class as a whole or this new code. I have excerted this code into another compiler and have tried to run it by itslef, Upon running it the console states it has encountered and error and closes. I have used the debugging mode on my compiler and it fails once it gets to the first line to create a new varalbe in the heap. I believe this is becasue all of my dynamic memory on my PC may be full as I get the "segmentation fault" error. I do not think that all the memory is in use by the other programs. If anything I should be able to make one Node in the memory. I just wanted to know before I change computers that it is in fact the memory being used and not some stupid mistake that I have made that is causing the problem. Any help would be fantastic! <code> #include <iostream> #include <cstdlib> #include <string> using namespace std; int main() { int numstep; struct step { int stepNum; string Instruction; }; struct Node { struct step steppy; Node *ptr_next_node; }; Node *Head; Node *Curr; Node *Last; numstep = 5; for( int i = 1; i <= numstep; i++) { (*Head).ptr_next_node = NULL; if((*Head).ptr_next_node == NULL) { Last = Head; Curr = Head; cout << " The " << i << "step, please enter the text for this setp and then press enter: "; cin >> (*Head).steppy.Instruction; (*Head).steppy.stepNum = i; (*Head).ptr_next_node = new Node; Curr = (*Head).ptr_next_node; } cout << "The " << i << " step, please enter the text for this step and then press enter: "; cin >> (*Curr).steppy.Instruction; (*Curr).steppy.stepNum = i; (*Curr).ptr_next_node = new Node; Last = Curr; Curr = (*Curr).ptr_next_node; } return 0;} </code> |
From: Michal M. <mol...@se...> - 2011-05-09 19:31:04
|
Dne 9.5.2011 17:44, Jordan Burnam napsal(a): > Node *Head; > Node *Curr; > Node *Last; > > > > > numstep = 5; > > > for( int i = 1; i <= numstep; i++) > { > (*Head).ptr_next_node = NULL; Here you are using Head but you haven't allocated it anywhere. There must be Head = new Node; before the for loop. > if((*Head).ptr_next_node == NULL) What is this good for? You just set it to NULL on previous line. > { > > Last = Head; > Curr = Head; > cout << " The " << i << "step, please enter the text for this setp and > then press enter: "; > cin >> (*Head).steppy.Instruction; > (*Head).steppy.stepNum = i; > (*Head)ptr_next_node = new Node; Probably missing . here. Note that (*x).y is the same as x->y in this case. > Curr = (*Head).ptr_next_node; > > } Here you probably intended "else". However it won't work because the if condition will always be true as explained above. > cout << "The " << i << " step, please enter the text for this step and > then press enter: "; > cin >> (*Curr).steppy.Instruction; > (*Curr).steppy.stepNum = i; > (*Curr).ptr_next_node = new Node; > Last = Curr; > Curr = (*Curr).ptr_next_node; =================================== Node *Head; Node *Curr; numstep = 5; Curr = Head = new Node; Head->ptr_next_node = NULL; for( int i = 1; i <= numstep; i++) { cout << " The " << i << "step, please enter the text for this setp and then press enter: "; cin >> Curr->steppy.Instruction; Curr->steppy.stepNum = i; Curr->ptr_next_node = i == numstep ? NULL : new Node; Curr = Curr->ptr_next_node; } Curr = Head; while(Curr) { cout << Curr->steppy.stepNum << " " << Curr->steppy.Instruction << endl; Curr = Curr->ptr_next_node; } |
From: Fernando L. <fer...@gm...> - 2011-05-09 22:17:11
|
Jordan, Head is a pointer, you use it before you assign anything to it, that is probably the cause of the "segmentation fault". It has to be assigned an actual address before you use it. Best regards, Fernando On 9 May 2011 12:44, Jordan Burnam <jor...@me...> wrote: > Hey Everyone and good morning, > > I am in the process of creating a to do list console application and seems > I have hit a wall so to speak. Below I have an exert from my actualy class. > I have been trying to get this linked list working and so far it has failed. > I did not know if it was the driver program that was failing, the class as a > whole or this new code. I have excerted this code into another compiler and > have tried to run it by itslef, Upon running it the console states it has > encountered and error and closes. I have used the debugging mode on my > compiler and it fails once it gets to the first line to create a new varalbe > in the heap. I believe this is becasue all of my dynamic memory on my PC may > be full as I get the "segmentation fault" error. I do not think that all the > memory is in use by the other programs. If anything I should be able to make > one Node in the memory. I just wanted to know before I change computers that > it is in fact the memory being used and not some stupid mistake that I have > made that is causing the problem. Any help would be fantastic! > > > <code> > > > #include <iostream> > #include <cstdlib> > #include <string> > > using namespace std; > > int main() > { > > int numstep; > struct step > { > int stepNum; > string Instruction; > }; > > struct Node > { > struct step steppy; > Node *ptr_next_node; > }; > Node *Head; > Node *Curr; > Node *Last; > > > > > numstep = 5; > > > for( int i = 1; i <= numstep; i++) > { > (*Head).ptr_next_node = NULL; > if((*Head).ptr_next_node == NULL) > { > > Last = Head; > Curr = Head; > cout << " The " << i << "step, please enter the text for this setp and > then press enter: "; > cin >> (*Head).steppy.Instruction; > (*Head).steppy.stepNum = i; > (*Head)ptr_next_node = new Node; > Curr = (*Head).ptr_next_node; > > } > > cout << "The " << i << " step, please enter the text for this step and > then press enter: "; > cin >> (*Curr).steppy.Instruction; > (*Curr).steppy.stepNum = i; > (*Curr).ptr_next_node = new Node; > Last = Curr; > Curr = (*Curr).ptr_next_node; > } > > > > > return 0;} > > </code> > > > ------------------------------------------------------------------------------ > WhatsUp Gold - Download Free Network Management Software > The most intuitive, comprehensive, and cost-effective network > management toolset available today. Delivers lowest initial > acquisition cost and overall TCO of any competing solution. > http://p.sf.net/sfu/whatsupgold-sd > > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > > |
From: Per W. <pw...@ia...> - 2011-05-11 20:49:56
|
What I can recommend here is that you really make yourself comfortable with the debugger of your choice. A couple of things: - Few programs will work on first attempt. Debugging is almost always needed. - With more experience, it will be easier and easier to just read code and see bugs so a very large percentage of bugs can be handled just by looking at symptoms and maybe adding some printouts to find out if the problem happens before or after something else. - In your case, you guess that you have a problem with your heap. It's important to not be too quick deciding what a problem is but always keep an open mind: What different problems may there be that makes a specific source code line or function call to fail. There are very often more than one potential reason, meaning that these alternatives have to be checked one-by-one. So very standard scientific methods to try to prove theories or prove that the theory was wrong. - Something you seem to constantly forget is to explicitly look at corner cases. What happens for the special case when you try to insert the first element in an empty list? What happens when you want to insert a new elemnt before the currently first element? What happens when you want to insert a new element after all other elements? - A very big question here - are you even building your source code with full warnings enabled? The compiler is very good at spotting uninitialized variables, unless the variables are global. Sometimes it takes a bit higher optimization to get the compiler to make more advanced flow-control analysis to figure out unreachable code or variables that can be reached without an initialization. - As already noted by other readers, you have source lines that assign a value to a specific variable and then directly the line after checks what value the variable has. Are you really working methodical and scientific when analyzing your program? Are you taking a break and then revisiting the code and single-stepping the code in your head, trying to see exactly what the different lines does and why you need the lines? A professional software developer normally don't have any web forums available for debugging their code. They may use a web forum or mailing list to try to ask for missing information or known workarounds to problems they have found. But actual debugging can't be offloaded to any web forum since the programs are too large and normally contains proprietary code that can't be posted. This means that a software developer really must spend the required time to try to master the craft of problem solving. Both solving the customers original problem - what program to write to fulfill a requirements specification. But also debugging written code to verify that it does work as expected and does solve the same problem as the requirements specification wanted solved. It really is very well invested time to work on the debugging skills. And what you will find in the end is that being a tiny bit slower when writing the code will save you a lot of time when trying to get the code to work as expected. So first think very carefully at the problem you have. Then decide how to solve it. Then write the code. Then look at the code and think: is this really good code for solving the problem? First them may it be meaningful to start any debugging. It is meaningless to debug code just to later realize that the code solved the wrong problem or is too slow or requires too much memory forcing you to rewrite it. Having to rewrite just means new code to debug. The interesting thing with your posts is that they just always contains a number of code lines and basically "it doesn't work". There is very little information showing exactly what work you have done to try to solve the problem. It should be quite easy to test if a memory allocation fails, or if it is the assign of the receied pointer that fails. There are almost always possible to divide a problem into two smaller problems until the bug is so totally cornered that you can see what is wrong and what needs to be done to fix the problem. Your posts does not show that you do subdivide your problems. When programs gets larger, you really must work that way or you will be unconditionally stuck since the number of potential alternatives will grow as a power function of the code size and only problem subdivision can compensate for the growing complexities. /pwm On Mon, 9 May 2011, Jordan Burnam wrote: > Hey Everyone and good morning, > > I am in the process of creating a to do list console application and > seems I have hit a wall so to speak. Below I have an exert from my > actualy class. I have been trying to get this linked list working and so > far it has failed. I did not know if it was the driver program that was > failing, the class as a whole or this new code. I have excerted this > code into another compiler and have tried to run it by itslef, Upon > running it the console states it has encountered and error and closes. I > have used the debugging mode on my compiler and it fails once it gets to > the first line to create a new varalbe in the heap. I believe this is > becasue all of my dynamic memory on my PC may be full as I get the > "segmentation fault" error. I do not think that all the memory is in use > by the other programs. If anything I should be able to make one Node in > the memory. I just wanted to know before I change computers that it is > in fact the memory being used and not some stupid mistake that I have > made that is causing the problem. Any help would be fantastic! > > > <code> > > > #include <iostream> > #include <cstdlib> > #include <string> > > using namespace std; > > int main() > { > > int numstep; > struct step > { > int stepNum; > string Instruction; > }; > > > struct Node > { > struct step steppy; > Node *ptr_next_node; > }; > Node *Head; > Node *Curr; > Node *Last; > > > > > numstep = 5; > > > for( int i = 1; i <= numstep; i++) > { > (*Head).ptr_next_node = NULL; > if((*Head).ptr_next_node == NULL) > { > > Last = Head; > Curr = Head; > cout << " The " << i << "step, please enter the text for this setp and then press enter: "; > cin >> (*Head).steppy.Instruction; > (*Head).steppy.stepNum = i; > (*Head).ptr_next_node = new Node; > Curr = (*Head).ptr_next_node; > > } > > cout << "The " << i << " step, please enter the text for this step and then press enter: "; > cin >> (*Curr).steppy.Instruction; > (*Curr).steppy.stepNum = i; > (*Curr).ptr_next_node = new Node; > Last = Curr; > Curr = (*Curr).ptr_next_node; > } > > > > > return 0;} > > > </code> |