C/C++ How to properly insert an element at the end of a linked list in C++?

  • Thread starter Thread starter rootX
  • Start date Start date
  • Tags Tags
    List Push
AI Thread Summary
The discussion revolves around the implementation of a `push_back` function in a singly linked list, specifically addressing the issue of memory management when adding new nodes. The primary concern is the inappropriate deletion of a newly created node after it is linked to the list. Participants emphasize that deleting the node immediately after linking it to the list would result in the loss of the data contained within that node. Clarifications are made regarding pointer management, particularly the distinction between deleting a pointer and the object it points to. It is noted that simply reassigning a pointer without invoking `delete` can lead to memory leaks if the original object is not properly deallocated. The discussion concludes with best practices for managing dynamic memory, highlighting the importance of deleting pointers before reassigning them to prevent memory leaks while ensuring that the data remains accessible.
rootX
Messages
478
Reaction score
4
Following inserts new element at the list end.
The class has list_head, list_tail, and count ...

Problem: I don't know if node should be deleted or not.

Code:
//inserts an element at the end
template <typename Object>
void Single_list<Object>::push_back( const Object & obj ) {
    //makes new node with value obj that has next element pointing 
    //to null
	Single_node<Object> *node = new Single_node<Object>(obj,0);
    //points the tail's next_node to the new node
    list_tail->next_node = node;
    //points list_tail to the new node
    list_tail = node;

    delete node;
    count++;
}
 
Technology news on Phys.org
I cannot imagine why you would delete a node when adding new data to a list. Here, you have created a new node, linked it to the list. Deleting that node will delete the data!
 
HallsofIvy said:
I cannot imagine why you would delete a node when adding new data to a list. Here, you have created a new node, linked it to the list. Deleting that node will delete the data!

For
Code:
Step 1: list_tail->next_node = node;
Step 2:   list_tail = node;
Step 3: delete node

Originally I had this:
[element 1] ... [element x] -> [element x->next] -> [element->next->next or list_tail] - >0Step 1 (Adding new data):
[element 1] ... [element x] -> [element x->next] -> [element->next->next or list_tail] -> [list_tail->next or node] ->0

Step 2 (Changing list_tail pointer):
[element 1] ... [element x] -> [element x->next] -> [element->next->next] -> [element->next->next->next or list tail or node] ->0

Step 3 (deleting node):
[element 1] ... [element x] -> [element x->next] -> [element->next->next] -> [element->next->next->next or list tail] ->0

I think node is just an extra in step 2 that's why I am deleting it
 
As HallsOfIvy said, "delete node" really means "delete *node" ie. the item which the node pointer is assigned to gets deleted. If that is your latest node, then you've just deleted it. If you really wanted to, you could assign *node to point to NULL when you're done with inserting the node. But that is unnecessary. Pointer "node" will cease to exist once the program exits the block of code.
 
Defennder said:
As HallsOfIvy said, "delete node" really means "delete *node" ie. the item which the node pointer is assigned to gets deleted. If that is your latest node, then you've just deleted it. If you really wanted to, you could assign *node to point to NULL when you're done with inserting the node. But that is unnecessary. Pointer "node" will cease to exist once the program exits the block of code.

ook thnx.
I thought delete pointer; deltes the pointer and *pointer if no other pointer is pointing to the same data.Second question:
If I have
Complex * object_pointer; // pointing to a complex object
and I do:

object_pointer = 0;

or

object_pointer = another_complex_object_pointer;

Does this mean that my *object_pointer would be deleted?
 
Hmm, you didn't invoke delete in either case. In the first one you are re-assigning object_pointer to NULL, whereas in the second case you are telling object_pointer to point to whatever another_complex_object_pointer is assigned to.

In either case, object_pointer no longer points to that which is originally assigned to. Let us denote that as "obj" If no pointer is assigned to "obj" and "obj" is dynamically created using "new" , then you have a memory leak which is simply data which cannot be accessed by your program but which takes up memory. But of course it's still there although you can't access it, which means that it's as good as deleted.
 
Defennder said:
Hmm, you didn't invoke delete in either case. In the first one you are re-assigning object_pointer to NULL, whereas in the second case you are telling object_pointer to point to whatever another_complex_object_pointer is assigned to.

In either case, object_pointer no longer points to that which is originally assigned to. Let us denote that as "obj" If no pointer is assigned to "obj" and "obj" is dynamically created using "new" , then you have a memory leak which is simply data which cannot be accessed by your program but which takes up memory. But of course it's still there although you can't access it, which means that it's as good as deleted.

so whenever I want to change pointer's complex data,
I should do:

delete pointer;
pointer = some_other_pointer;

I think this is better:
//don't create new pointer to store new data

*pointer = new_data; //so the original data is deleted?

I don't want any memory leaks. Currently, My +10 functions program allocates memory but does not delete all of it.
 
I don't follow what you're trying to do. If pointer is assigned to data and you delete it, how then are you going to change it?

And for your second case, you have to assign a pointer to data created using "new" keyword, or else you have a memory leak.
 

Similar threads

Replies
2
Views
2K
Replies
1
Views
1K
Replies
3
Views
8K
Replies
9
Views
3K
Replies
31
Views
3K
Replies
6
Views
13K
Back
Top