![]() |
| | #1 |
| Registered User Join Date: May 2009
Posts: 83
| Question related to linked lists Code: struct node {
int x;
node *next;
};
int main()
{
node *root; // This won't change, or we would lose the list in memory
node *conductor; // This will point to each node as it traverses the list
root = new node; // Sets it to actually point to something
And I'm assuming, since its the "new" keyword, that this new object/structure/or-whatever-you-want-to-call-it is created in the "heap" section of memory, correct? Why, then, is there no need for the delete keyword to delete the "root" variable? |
| Programmer_P is offline | |
| | #2 |
| and the Hat of Ass Join Date: Dec 2007
Posts: 731
| It allocates memory for a new node struct and sets root to point to that newly-allocated node. It is created on the heap, and you SHOULD delete the root when complete. If you "new" something, consider yourself responsible for "delete"-ing it as well. |
| rags_to_riches is offline | |
| | #3 | |
| Registered User Join Date: May 2009
Posts: 83
| Quote:
Thanks for the reply. | |
| Programmer_P is offline | |
| | #4 |
| Registered User Join Date: May 2009
Posts: 83
| So, does this code look good then? Programming_Exercises.h: Code: struct node {
string store; //for storing something in the list
node* next;
int one;
int two;
int three;
int random;
};
Code: #include <iostream>
#include <string.h>
#include "Programming_Exercises.h"
using namespace std;
int main() {
node* root; //This will be the unchanging first node
node* conductor; //This will point to each node as it traverses the list
root = new node;
//Assisn values:
root->next = 0; //otherwise it wont work well
root->store = "This program tests a linked list.\n\nYes, it tests a linked list.";
root->one = 1;
root->two = 2;
root->three = 3;
root->random = 4;
conductor = root; //The conductor points at the first node
if (conductor != 0 ) {
while (conductor->next !=0) {
cout<< conductor->store;
cout<< conductor->one <<", " << two << ", " << three << ", " << random <<endl;
conductor->random = 5;
cout<< conductor->random;
conductor = conductor->next;
}
cout<< conductor->store;
}
conductor->next = new node; //creates a node at the end of the list
conductor = conductor->next; //Points to that node
conductor->next = 0; //Prevents it from going further
conductor->store = "Conductor just accessed the list, changed the contents of the string...\n\nTest completed \"
"successfully. Thank you for trying this program";
cout<< conductor->store;
delete[] root;
return 0;
}
|
| Programmer_P is offline | |
| | #5 |
| and the Hat of Ass Join Date: Dec 2007
Posts: 731
| If you new it, you should delete it. The memory allocated by new is returned to the operating system upon exit of the program, so strictly speaking the author is not incorrect in the provided, simple example. However, it is GOOD FORM AND PRACTICE to clean up after yourself! You must use the array delete operator -- delete [] -- only on arrays, not on pointers to non-array objects. root is a node object, and therefore should not be deleted as if it were an array, it should be deleted with delete root. Any nodes under root ARE NOT DELETED simply by deleting root. To properly clean up you must traverse the list node by node and delete each one. Your while loop will not be entered, as you've not added anything to the list. |
| rags_to_riches is offline | |
| | #6 | |||
| Registered User Join Date: May 2009
Posts: 83
| Quote:
Quote:
Quote:
Thanks for the replies. | |||
| Programmer_P is offline | |
| | #7 |
| Registered User Join Date: May 2009
Posts: 83
| Ok, this is ridiculous... Each new (NOT the keyword new) node I try to create has to be pointed at something, so I don't leave any hanging pointers, or initialized to a NULL (0) value, but the problem is the while loop will NEVER be entered no matter what, because the condition (conductor->next != 0) will never be met, because its always going to have a NULL value... For example, I have pointers *x and *y: I point *x AT *y...so what does y get pointed at? Nothing! And so I say y = 0. Only problem is I said (figuratively), "You can only enter this while loop while *x does NOT equal 0". And so, unfortunately, even though x = y, y = 0, and so x = 0. So how do I solve the problem? Use some other condition? ![]() I guess I don't understand why the author of that tutorial put that condition in there in the first place. (To translate: I attempted to create a new node (naming it secondNode, by convention), and assign it 0 -- so, node* secondNode = 0; I then did: root->next = secondNode; Now, the problem... if (conductor != 0) { while (conductor->next != 0) { //Do stuff } Since conductor->next (keep in mind "next" is a node pointer in the node struct) is pointing at secondNode (another pointer, which has been initialized to 0), the condition is not met, and so it can't enter the while loop. ) Last edited by Programmer_P; 11-02-2009 at 03:17 PM. |
| Programmer_P is offline | |
| | #8 | |
| Registered User Join Date: Sep 2004 Location: California
Posts: 2,845
| Quote:
Code: // Create a linked list that is 3 nodes in length
node* conductor = new node;
node* second = new node;
node* third = new node;
conductor->next = second;
second->next = third;
third->next = NULL;
while(conductor->next != NULL)
...
__________________ bit∙hub [bit-huhb] n. A source and destination for information. | |
| bithub is offline | |
| | #9 |
| Registered User Join Date: May 2009
Posts: 83
| Oh...I see. I need the new keyword still. Thanks! |
| Programmer_P is offline | |
| | #10 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,381
| Considering that just about every program out there has a few memory leaks in it, how could your computer ever stay up for more than a few minutes if the OS did NOT clean up the mess?
__________________ "Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot |
| brewbuck is offline | |
| | #11 | |
| Registered User Join Date: May 2009
Posts: 83
| Quote:
But, then again...I'm sure its best to follow the "delete every new object" rule. Some OSes are probably coded worse than others though...I'm sure Windows, for example, probably does a poorer job of cleaning up than any other OS. | |
| Programmer_P is offline | |
| | #12 | |
| Ex scientia vera Join Date: Sep 2007
Posts: 426
| Quote:
Without having any proof or benchmarks or anything similar, I have to say that particularly vista is unbelievably slow at cleaning up memory upon the execution of a big program. I see this even on new installations.
__________________ "What's up, Doc?" "'Up' is a relative concept. It has no intrinsic value." | |
| IceDane is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Question About Linked Lists Example | bengreenwood | C Programming | 6 | 08-07-2009 11:45 AM |
| Question regarding comparing two linked lists | cyberfish | C++ Programming | 2 | 08-23-2007 04:28 AM |
| Singly Linked Lists: Clarification Needed | jedispy | C++ Programming | 4 | 12-14-2006 05:30 PM |
| Linked Lists Question | panfilero | C Programming | 4 | 11-22-2005 01:33 AM |
| Linked Lists Question | SlyMaelstrom | C++ Programming | 12 | 11-12-2005 12:03 PM |