0
void createnode(int data)
{
    node *temp=new node;
    temp->data=data;
    if(head==null)
    {
        head=temp;
        tail=temp;
        temp=null;
    }
    else
    {
        tail->next=temp;
        temp=null;
    }
}

Should I delete temp right now? I'll be using another function to delete every node. Will that be enough?

2
  • What you asking us is if you should delete null, which is its value at that point in time. That you shouldn't try. You should read into what a pointer is. delete is for removing a variable that is stored on the heap, which you do when you don't need that part of the memory anymore. In your case, when you need to remove a node.
    – Aziuth
    Jun 23, 2018 at 13:55
  • 1
    @Aziuth note: delete(nullptr); is harmless. Jun 23, 2018 at 13:58

3 Answers 3

0

Should I delete temp right now? I'll be using another function to delete every node.

No.

As your function name suggests, it is createNode() (read as create node).

The whole point for this function is to construct your list by creating and linking nodes;
NOT delete them.

Delete only when the list is destructed or you want to remove (or delete) a node from the list.

NOTE:

Rename temp to "newNode" and don't set it to null in order not to have dangling pointers.

-1

First of all, temp is null, so deleting it would be a no-op.

If you're asking whether you should delete the result of new node in this function, the answer is "no, you should not". If you did, the list would retain dangling pointers. The correct way is delete the node when it's being removed from the list, when the list itself is being deleted, etc.

1
  • Using smart pointers would be smarter. Manual memory management is a code smell in modern C++. Jun 23, 2018 at 13:59
-1

You need to read into some basic stuff about pointers and memory.

delete is used to remove something from the memory. In that case, that is the heap variable that stores the content from your node. Obviously, that is to be done exactly when the node is deleted.

After you set temp to nullptr, it does not represent (/point to) any memory anymore. Calling delete on it would make no sense. (It's technically harmless, but having useless lines in your code is still not good.)

I recommend for you to read into what the stack and the heap are and why the heap needs new and delete while the stack doesn't. That might clear things up for you.

4
  • delete doesn't remove things from memory - it calls the destructor for an object and then returns the memory to the memory allocation system. And the ability to call delete safely on a null pointer can simplify some programs.
    – user2100815
    Jun 23, 2018 at 14:05
  • @NeilButterworth Is this helpful to a beginner? I simplified, yes, but that is the intended result of the call.
    – Aziuth
    Jun 23, 2018 at 14:08
  • Correct information is always more helpful than incorrect information
    – user2100815
    Jun 23, 2018 at 14:09
  • @NeilButterworth Unnecessary details are not more helpful than focusing on the important aspects. And please point at what you call incorrect information. I wrote "delete is used to remove something from the memory", which is true. Wrong would be "delete removes something from the memory".
    – Aziuth
    Jun 23, 2018 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.