Reputation: 1656
I was implementing a LinkedList using C++, and I seem to have forgotten a few things when dealing with dynamically allocated memory.
I have a node class:
class Node {
public:
Node(int d) {
data = d;
next = NULL;
}
Node(int d, Node* n) {
data = d;
next = n;
}
int data;
Node* next;
};
and in my LinkedList class, I have the following method:
void remove(int n) {
Node* current;
current = head;
Node* previous = NULL;
while ( current->data != n && current->next != NULL) {
previous = current;
current = current->next;
}
if (current->data == n) {
previous->next = current->next;
current->next = NULL;
delete current;
}
else {
std::cout << "Node not found" << std::endl;
}
}
I seem to have forgotten..When I do delete current
does that delete the Node
? Like the actual object that the pointer current
points to? Or does it just delete the pointer? Or does the deletion of a pointer pointing to dynamically allocated memory using delete
delete both the pointer and the object? Or do I need to have defined a Node class destructor for that?
Upvotes: 0
Views: 129
Reputation: 31
Whenever you call delete
on a pointer variable, the object to which it is pointing to gets deleted from the memory, however the 4 bytes allocated to the actual pointer variable (in your case, the current
variable), the 4 bytes will be freed only when the variable will go out of scope, ie At the end of the function
Upvotes: 0
Reputation: 11
It does delete the actual structure pointed to by current
. Pointers remain intact. No need for defining destructor.
The delete
operator is to be applied to pointer to object. The pointer is an address of memory on heap allocated by calling new
. Internally there is just table of addresses allocated by new
. So the key to free such memory is just that address. In your case such address is stored in variable of type pointer to Node named current
.
There are few problems in your code. The problematic one is that you have no posibility to tell whether node stored in current is actually allocated on heap. It might happen that current node is allocated on stack. E.g.
void someFunction(LinkedList &list) {
Node myLocalNode(10);
list.add(&myLocalNode);
list.remove(10); //<-- disaster happens here
}
The same applies to statically allocated global variables.
You must take care of extreme cases. Think about what happens when deleted object is the first one, pointed by variable head
. By deleteing its memory you end up with dangling pointer in head
, pointing to either unallocated memory or memory used by someone else.
My third objection is to writing such structure at all. I hope it is just some school excercise, because in any other cases you should (almost must) use some existing list like std::list
from C++ STL.
Upvotes: 0
Reputation: 3125
Assuming that you have allocated your object using new delete on a pointer does the following:
At some point the memory manager will free and mark it as non-accessible by the process.
Thus it is up to you to set the pointer after calling delete to an agreed value. The best practice it to set it as nullptr for the latest compilers.
Upvotes: 0
Reputation: 119847
delete p
causes the object pointed to by p
to cease to exist. This means that
1, If the object has a destructor, it is called; and
2. p
becomes an invalid pointer, so that any attempt to dereference it is undefined behaviour.
Generally, the memory occupied by said object becomes available to the program again, though this is really an implementation detail.
The phrase "delete the pointer" is normally a sloppy shorthand for "delete the object pointed-to by the pointer".
Upvotes: 0
Reputation: 1442
Delete just free's the memory pointed to. This has the following implications:
Upvotes: 0
Reputation: 667
It just deletes the struct -in your case node- it points to, you can still use that pointer -make it point to another node-, in fact there's no way delete the pointer itself since it's allocated on the stack. it's automatically "deleted" when you leave the function.
p.s: no need to set current->next to null
Upvotes: 2