Free memory with pointer?

Say I allocate memory a linked list like this one.

struct linkedList{
stuff...
linkedList* p_nextItem = NULL;
}

for (int i = 0; i < 10; i++{
linkedList* p_myLinkedList = addNewItem();
}

Can I then just create a pointer to a linkedList, say to the sixth item in the linked list, then free the memory via that pointer?

Example: linkedList* p_toList = p_myLinkedList; //p_myLinkedList is the sixth item.

delete p_toList;

Would the sixth item be correctly freed after this?





closed account (E0p9LyTq)
From what little you've shown you will free the memory, but your linked list will still retain the reference (pointer) to the memory location. Unexpected behavior should you try to access that list element!

Linked lists should take care of allocating and freeing memory when creating and deleting list items as part of the details of the inner workings of the list and list nodes.
Yes, this was only a short example to clearify my question. The list is far from complete, but I only wondered about the memory. Thanks!
Topic archived. No new replies allowed.