Pointers, linked lists and other horrors ...

Hello, I constructed a doubly linked list to keep double values in a dynamically allocated manner. Then I wrote push_front and push_back methods to add a new elements at the beginnign and the back of the list. The code of the push_front looks like that:
// create a new temporary element of the Element type
Element * Temp = new Element;
// change its previous pointer to point at list head
// the next pointer is null (first element in the list)
// the value is set to the parameter passed to the method
// the next pointer in the head should point at the Temp
Temp->previous = head;
Temp->next = NULL;
Temp->Item = Value;
head->next = Temp;
// now change the head Element
head = Temp;
// increase the number of elements in the list
iCount++;
// delete the Temp element pointer
delete &Temp;
The lastline causes confusion. Since I used a new operator to create an instance of the Element class (which contains two pointers - next and previous, and a data field - Item), should I delete the pointer by hand with the delete operand or leave the compiler to do teh rest? Does the scope end automatically delete the pointer? If what I used is wrong what is the proper form of the red line? The pointee should be left intact since this will build up the body of the linked list.
Thank You for Your feedback.
Best wishes
Marek

Comments

  • : Hello, I constructed a doubly linked list to keep double values in a dynamically allocated manner. Then I wrote push_front and push_back methods to add a new elements at the beginnign and the back of the list. The code of the push_front looks like that:
    : // create a new temporary element of the Element type
    : Element * Temp = new Element;
    : // change its previous pointer to point at list head
    : // the next pointer is null (first element in the list)
    : // the value is set to the parameter passed to the method
    : // the next pointer in the head should point at the Temp
    : Temp->previous = head;
    : Temp->next = NULL;
    : Temp->Item = Value;
    : head->next = Temp;
    : // now change the head Element
    : head = Temp;
    : // increase the number of elements in the list
    : iCount++;
    : // delete the Temp element pointer
    : delete &Temp;
    : The lastline causes confusion. Since I used a new operator to create an instance of the Element class (which contains two pointers - next and previous, and a data field - Item), should I delete the pointer by hand with the delete operand or leave the compiler to do teh rest? Does the scope end automatically delete the pointer? If what I used is wrong what is the proper form of the red line? The pointee should be left intact since this will build up the body of the linked list.
    : Thank You for Your feedback.
    : Best wishes
    : Marek
    :
    :

  • : : Hello, I constructed a doubly linked list to keep double values in a dynamically allocated manner. Then I wrote push_front and push_back methods to add a new elements at the beginnign and the back of the list. The code of the push_front looks like that:
    : : // create a new temporary element of the Element type
    : : Element * Temp = new Element;
    : : // change its previous pointer to point at list head
    : : // the next pointer is null (first element in the list)
    : : // the value is set to the parameter passed to the method
    : : // the next pointer in the head should point at the Temp
    : : Temp->previous = head;
    : : Temp->next = NULL;
    : : Temp->Item = Value;
    : : head->next = Temp;
    : : // now change the head Element
    : : head = Temp;
    : : // increase the number of elements in the list
    : : iCount++;
    : : // delete the Temp element pointer
    : : delete &Temp;
    : : The lastline causes confusion. Since I used a new operator to create an instance of the Element class (which contains two pointers - next and previous, and a data field - Item), should I delete the pointer by hand with the delete operand or leave the compiler to do teh rest? Does the scope end automatically delete the pointer? If what I used is wrong what is the proper form of the red line? The pointee should be left intact since this will build up the body of the linked list.
    : : Thank You for Your feedback.
    : : Best wishes
    : : Marek
    : :
    : :
    :
    :
    regardless if I agree this code or not
    i think you need to change the last line to
    delete Temp;
    the address operator is used to give the address of a variable but Temp is already a pointer i.e it is an address you must not use &
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion