Could you tell, why the implementation of the next code leads to the error:
void del (num*&head) {
num*temp = 0;
if (head!=0) {
temp = head;
delete temp;
del (head->next);
}
else return;
}
The error:
Access violation reading location 0xcdcdcdc1.
Whereas next code works properly:
void del (num*&head) {
if (head!=0) {
del (head->next);
delete head;
}
else return;
}
Deleting temp invalidates head, since they both point to the same object. head->next tries to access the deleted object, giving undefined behaviour. You should instead store head->next, so you don't have to access the deleted object:
if (head) {
num * temp = head->next;
delete head;
del (temp);
}
You're running a debug build, so the deleted memory was helpfully set to garbage, rather than left with its old contents. This means that trying to use values read from it as pointers will quickly trigger an access violation, helping you find the source of the problem.
The second version waits until you've finished with the object before deleting it, so there's no undefined behaviour.
However, recursion is a bad idea since it could cause a stack overflow if the list is too long. Iteration would be safer:
while (head) {
num * temp = head->next;
delete head;
head = temp;
}
You're accessing head->next while actually you already deleted head the line before.
Another way to go about this would be:
void del (num*&head) {
if (head!=0) {
num* temp = head->next;
delete head;
del (temp);
}
else return;
}
In the first version,
temp = head;
delete temp;
del (head->next);
temp and head point to the same node, then the node is deleted and becomes invalid. But it is accessed in the last line with
del (head->next);
The second version does not have this problem.
temp = head;
delete temp; // head is dead too!
del (head->next); // dereferencing disposed pointer
As far as you operate with pointers pointing to the same memory, deleting any of that pointers would make the memory deleted. So, even if you don't delete that other pointers they point to a piece of trash (or even to memory owned by another person)
Think of it in this way:-
when you have done
temp = head;// it means temp and head are talking about the same person,i.e, the same memory location.
delete temp; //the person(memory location) to which the both of them are pointing is dead;
del (head->next); //Now you trying to kill the "ghost" of the person means you are trying to delete already deleted node.
But in your second case the recursive call is just above
delete head;
so the recursive call first progresses towards the end and then nodes are deleted from the end towards the start, as follows:-
if you have n nodes, the summary of recursive call can be viewed as:-
del (list); // call from main.
del (list->head); //recursive call.
del (list->head->head);//recursive call.
del (list->head->head->head);//recursive call.
so on..................
so when last node appears its next is NULL so recursion stops and the nodes are deleted from the last call to the first call fashion in other words from the last node towards the first node. So no harm is done here as in your first case.
Related
I am making a modified double ended Linked List, but the head and the tail point to each other. In the insertBeforeCurrent and the insertAfterCurrent I defined the objects with new and put them into the Linked List. But when I go to use delete, the program just crashes. I have done some tests, and insertBeforeCurrent and insertAfterCurrent works, and I am able to through the Linked List and print every single element with the getPrevious and getNext. I also printed it using only insertBeforeCurrent, insertAfterCurrent and I was also able to do the same with the mixture of the two. I was able to print it with 1,2,3,and 6 elements in the Linked List. The problem I have is the debugger, everything works until I hit the delete temp; at which point it will just say.
Can't find a source file at "/build/glibc-t7JzpG/glibc-2.30/signal/../sysdeps/unix/sysv/linux/raise.c"
Locate the file or edit the source lookup path to include its location.
I know you can only use the delete function for delete the data created by the new's dynamic memory allocation, but that doesn't seem to be the case since every element of the Linked List is created by new.
So the issue with application crashing is not the Node * x = new Node(); followed by x = y;. These don't show any warnings,the application runs, and 5 or 6 people have pointed them out. Thank you by the way. My issue is specifically is the delete temp;and why is it not being deleted. I have left the code for some context.
EDIT: I have removed the insertBeforeCurrent and insertAfterCurrent code since it is not needed.
bool CircularDoublyLinkedList::remove(int original_data)
{
Node search_data = search(original_data);
Node* temp = &search_data;
Node* current_next;
Node* current_previous;
if (temp != NULL)
{
if (temp == head)
{
current_previous = temp->getPrevious();
current_next = temp->getNext();
current_previous->setNext(current_next);
current_next->setPrevious(current_previous);
head = current_next;
temp->setNext(NULL);
temp->setPrevious(NULL);
delete temp;
current = current_next;
cout << "Delete successful." << endl;
}
else if (temp == tail)
{
current_previous = temp->getPrevious();
current_next = temp->getNext();
current_next->setPrevious(current_previous);
current_previous->setNext(current_next);
tail = current_next;
temp->setNext(NULL);
temp->setPrevious(NULL);
delete temp;
current = current_next;
cout << "Delete successful." << endl;
}
else
{
current_previous = temp->getPrevious();
current_next = temp->getNext();
current_previous->setNext(current_next);
current_next->setPrevious(current_previous);
temp->setNext(NULL);
temp->setPrevious(NULL);
delete temp;
}
return true;
}
return false;
}
I know you can only use the delete function for delete the data created by the new's dynamic memory allocation,
So far so good.
but that doesn't seem to be the case since every element of the Linked List is created by new.
This is not relevant, since the temp in CircularDoublyLinkedList::remove() never points to an element of the linked list. You assign to temp the address of a local variable (search_data) and never change that. Local variables are not created by new, so (as you noted earlier), you cannot delete &search_data.
(You might have noticed this yourself had you used a debugger to step through your code while trying to delete the first element of the list. In your current code, temp == head will never be true, even though that is the branch for deleting the first element. Similarly, temp == tail will never be true, and temp != NULL will never be false.)
At a guess, your search() function should probably return a pointer to a node in the list instead of returning the node, at which point you'll no longer need the (poorly-named) temp variable.
I'm having an issue with deleting pointers. I don't think I'm doing anything compiler illegal or anything, but perhaps I am, so I would appreciate it if someone could explain the flaw in my logic. I'm hoping the below function should be enough to help, as the whole thing would be a lot to transcribe, but if any more of the code is required, please let me know and I'll add it!
The below is a function to remove lockers from a linked list I've created. I've done my best to cover every conceivable case. The problem arises when I try to actually deallocate the memory of a locker I want to delete. The lines where I've tried to delete the temp variable that references that locked are commented out, because the code breaks with them included. Obviously, though, without them, I can't delete the lockers like I want.
int SelfStorageList::removeLockersOverdue() {
int lockersDeleted = 0;
if (isEmpty()) {
return 0;
}
if (head->objLocker.isRentOverdue && head==tail) { //If that was the only locker, the tail needs to be updated to = head = 0
delete head;
head = tail = 0;
return ++lockersDeleted;
}
LockerNode *prev = head;
LockerNode *curr = head->next;
while (curr != 0) {
if((curr == tail) && curr->objLocker.isRentOverdue) { //If the current locker is tail and needs deleting
LockerNode *temp = curr;
curr = prev;
//delete temp;
lockersDeleted++;
}
if(prev->objLocker.isRentOverdue) { //General case: Previous locker needs deleting
LockerNode *temp = prev;
prev = prev->next;
curr = curr->next;
//delete temp;
lockersDeleted++;
}
else { //Update the pointers if not updated anywhere else
prev = prev->next;
curr = curr->next;
}
}
return lockersDeleted;
}
Any "pointers"? (Terrible pun. :p )
Because you're maintaining a singly-linked list, I see you're keeping track of a prev pointer as you're iterating through your list. That's as it should be, of course, since you can't get the previous node of a given node in a linked list if it's singly-linked without remembering what its previous node was. Your problem is simply that your logic is busted: If a deletion is needed, you need to be deleting the curr node, and patching up the prev node to point its next pointer to curr->next before you delete curr.
Think about it: What you're doing is deleting the prev node, but there's likely a "more previous than that" node that's still pointing to the prev node that you just deleted. The next time you iterate through the list, you'll be iterating into formerly allocated nodes, which may be allocated for some entirely different purpose by that point. Your memory allocator is failing some internal assertion because likely on the next time you call removeLockersOverdue(), the memory has NOT yet been allocated to something else, and you're still finding the same node there that you already deleted, and finding again that it's overdue, and deleting it again, and your memory allocator is complaining that you're deleting memory that isn't allocated. (It would be really nice if it gave you that clear of a message, wouldn't it!)
Also, your special case stuff for handling the very first node & last node can be simplified & commonized; I'll avoid rewriting it for you so you can see if you can simplify it yourself.
while (curr != 0) {
if((curr == tail) && curr->objLocker.isRentOverdue) {
LockerNode *temp = curr;
curr = prev;
//delete temp;
lockersDeleted++;
}
You are not updating the previous node's next pointer here, so it ends up dangling (pointing to memory that is deleted). You also leave tail pointing at the deleted node.
if(prev->objLocker.isRentOverdue) {
LockerNode *temp = prev;
prev = prev->next;
curr = curr->next;
//delete temp;
lockersDeleted++;
}
Again, you don't update the next pointer in the list that points at the deleted node.
else { //Update the pointers if not updated anywhere else
prev = prev->next;
curr = curr->next;
}
}
I think you are greatly confusing yourself by carrying around two pointers in the loop. Try to write it again with just a single pointer tracking the node you are currently examining, and draw some pictures of the three possible cases (remove first node, remove last node, remove internal node) to ensure you are getting the removal logic right.
I coded the following. Insert, delete at beginning, insert at beginning and end are all working fine. The memory marked in bold is not freed up. Should cout<<temp give an error? Please comment on the correctness of this code.
void del(node** head)
{
node* temp = (*head)->next;
node* prev = *head;
while(temp ->next!= NULL)
{
prev = temp;
temp = temp -> next;
}
cout<<endl<<temp;
cout<<endl<<prev;
//delete prev ->next;
prev -> next = 0;
delete temp;
cout<<endl<<"temp after free"<<temp;
cout<<endl<<prev;
}
void main()
{
node* head = NULL;
int x = 5;
head = insert(head,x);
insert(head,6);
insert(head,7);
insert(head,8);
print(head);
del(&head);
print(head);
getch(); }
Output:
Empty List
|5| at 00673BF0
-------------------
|6| at 00673C30
-------------------
|7| at 00673FB8
-------------------
|8| at 00673FF8
-------------------
00673FF8
00673FB8
temp after free00673FF8
00673FB8
|5| at 00673BF0
-------------------
|6| at 00673C30
-------------------
|7| at 00673FB8
-------------------
delete does not set the value of the pointer to NULL, but the memory pointed to is no longer valid (does not contain a live node)
This means that cout << temp will print the value of the pointer it still has (and was the address of the node before the delete), but dereferencing temp (e.g. *temp or temp->next) is undefined behavior
Note: nowehere in del do you modify the pointer pointed to by head, so you either don't need the double indirection (node**) or you should assign the new head to *head.
As Attila already pointed out it's undefined behavior to dereference a pointer after deleting it. To prevent accidentally doing that it's good practice to assign NULL to the pointer directly after the delete. Dereferencing a NULL pointer immediately causes an error pointing you to the right cause.
In the code its good to free the temp node first. Then assign the prev->next to NULL. So that it will be very easy to understand that temp node is not available, thats way making prev->next NULL.
I have a linked list contains 3 nodes like the image shown:
There is a head pointer and temp1 pointer point to the front of the list, and tail point points at the end of the list.
I want to remove all the nodes, and change it back to its original initial form ( tail = NULL, head = first_node , but the first node doesn't have any value in the data and next field).
Because I want to start putting up some new values in it. To remove all those data, is this code going to remove nodes inside this linked list and left with the first node with no values in data and next field?
This code is in C++:
while(temp1!=tail)
{
temp1 = temp1->next;
if(temp1->next == tail)
{
tail=temp1;
temp1 = temp1->next;
free(temp1);
}
}
But then, does this mean only the last node will be deleted? are there any way to delete all the nodes except the first one?
To delete all nodes except the first node, you can try below code.
temp1 = head->next;
while(temp1!=NULL) // as I am considering tail->next = NULL
{
head->next = temp1->next;
temp1->next = NULL;
free(temp1);
temp1 = head->next;
}
This will delete all nodes except first one. But the data with the first node will remain as it is.
Disclaimer: I assume it's only for learning purposes and in real-world scenario you would use std::list<> or similar container.
For single-linked list, you can just drop all this burden an let the stdlib manage the pointers:
class Node {
std::unique_ptr<Node> next;
};
You can safely use .reset() method to make operations on the list:
Given current_ptr, the pointer that was managed by *this, performs the following actions, in this order:
Saves a copy of the current pointer old_ptr = current_ptr
Overwrites the current pointer with the argument current_ptr = ptr
If the old pointer was non-empty, deletes the previously managed object if(old_ptr != nullptr) get_deleter()(old_ptr).
From http://en.cppreference.com/w/cpp/memory/unique_ptr/reset.
And that's pretty much what you would do when deleting. I believe you can also use unique_ptr::swap(), to easily manipulate your nodes.
Instead of free, C++ uses delete function.
Check the link to have deep knowledge about all kind of operations(including recursive or iterative delete) on linked lists.
temp1 = head->next;
while(temp1!=NULL) // as I am considering tail->next = NULL
{
head->next = temp1->next;
temp1->next = NULL;
free(temp1);
temp1 = head->next;
}
The logic for this would be more correct if it is this way.
After the statement
free(temp1);
Add the condition
if (head -> next != NULL)
temp1 = head->next;
Since after deleting the last node there is no point in reassigning the address of head pointer to temp1.
I am currently writing stack that is being implemented with a linked list. I am get this error:
Unhandled exception at 0x75249617 in STACK_LinkedList.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x002ee8f8.
I believe it is possibly coming from either my push() or pop() functions. I can't find my error. I am fairly new to linked lists, so I have a little bit of a tough time finding errors.
Here is my push() function:
// Adds an item to the top of the stack
template <class S>
void Stack<S>::push(const S & e)
{
NodePointer temp = new Node(e);
if ( isEmpty() )
{
theTop = theFront = temp;
}
else
{
// Whatever is after top is stored in temp to keep track of it
theTop->next = temp;
// TheTop is stored in temp
theTop = temp;
delete temp;
}
}
Here is my pop() function:
//Takes the item off the top of the stack
template <class S>
void Stack<S>::pop()
{
if ( !isEmpty() )
{
//temp node is set equal to the front
NodePointer temp = theFront;
//Holds the second to last node in the linked list
NodePointer pred;
//loops through until the node after temp is 0
while (temp->next != 0)
{
//sets the second to last as temp
pred = temp ;
//basically "increments" temp to the next node
temp = temp->next ;
}
//sets temp equal to the top
temp = theTop;
//the top is then set to its predecessor
theTop = pred;
//deletes what was known as the top
delete temp;
}
else
cout << "STACK IS EMPTY" << endl;
}
Thanks alot! I believe most of my logic is correct. I just am missing something small. If it's something else please tell me and i'll post that code.
You should not delete your temp in push! It's a part of the list. So when you access this data later, you get surely an exception.
Second, you have to initialize your pred with NULL in pop(), otherwise you'll get an undefined value assigned to theTop if the stack contains only 1 item.
Third, you should delete in pop() the Node which you allocated in push().
In general, your approach seems to be not very efficient. You should better store the pointers other way round: from stack top to the bottom items. That way you won't need to traverse the whole stack on each pop(). Your code will be something like that:
void push(data)
{
allocate new top
new top's next is the old top
store new top in the class
}
void pop()
{
if empty, ERROR;
new top = old top's next
deallocate old top
}
Note that you don't need theFront at all.
Your push function is deleting "temp". However, temp points to the data you just added to your list. If call delete on a pointer, you are not throwing away the pointer, but rather deleting the memory it points to! Get rid of your delete statement in push and test that first (without pop). I haven't looked over your pop function, but I will leave that as an exercise for you to check for errors after you test pop().
-Dan8080