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

  • Context: C/C++ 
  • Thread starter Thread starter rootX
  • Start date Start date
  • Tags Tags
    List Push
Click For Summary

Discussion Overview

The discussion revolves around the implementation of a function to insert an element at the end of a linked list in C++. Participants are examining the handling of memory, particularly the deletion of nodes, and the implications of pointer reassignment in dynamic memory management.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant presents a code snippet for inserting a new node at the end of a linked list but expresses uncertainty about whether the newly created node should be deleted after insertion.
  • Another participant argues that deleting the node immediately after linking it to the list would result in losing the data, questioning the logic behind such an action.
  • A later reply clarifies that the statement "delete node" refers to deleting the object the node points to, not the pointer itself, and emphasizes that the pointer will cease to exist after exiting the block of code.
  • Further discussion includes a participant's confusion regarding pointer reassignment and memory leaks, noting that failing to delete dynamically allocated memory can lead to memory leaks.
  • Another participant suggests that to avoid memory leaks, one should delete the pointer before reassigning it to a new object, but questions arise about how to change the data without losing the reference.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the proper handling of node deletion and pointer reassignment, with multiple competing views on memory management practices and the implications of deleting pointers versus the objects they point to.

Contextual Notes

There are unresolved assumptions regarding memory management practices, particularly concerning the lifecycle of dynamically allocated objects and the potential for memory leaks when pointers are reassigned without proper deletion.

rootX
Messages
480
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 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
8K
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 31 ·
2
Replies
31
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
89
Views
7K