-1

I am learning Linked list data structure in C++, So I wrote this code which deletes a node with specific data value(key).

void deleteNodeK(Node** headref, int key){
    
    Node* temp = *headref;
    Node* prev = NULL;
    
    while(temp->data!=key && temp!=NULL){
        prev = temp;
        temp = temp->next;
    }
    if(temp==NULL){
        return;
    }
      prev->next = temp->next;    
    
    delete temp; //or free(temp);
}

As you can see the last line says delete temp; but some websites have the same code but with free(temp).

Which one of them is correct or better to use in C++?

Evg
  • 25,259
  • 5
  • 41
  • 83
Hill
  • 1
  • 1
  • In your case these 2 are similar. `delete()` calls destructor of the class and `free()` does not. As generally linked list node does not have destructor it does not matter which one you use. It, of course, it is different if you use destructor and wants call it before deleting the object. – Syed Nakib Hossain Jul 11 '21 at 14:15
  • 5
    They are not at all interchangeable. Which you use depends on how you created the node to begin with. `delete` **only** what you `new`. Whereas `free` is only for use with `malloc`. – StoryTeller - Unslander Monica Jul 11 '21 at 14:17
  • Start not with data structures, but with language basis by reading some [introductory book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). It doesn't make sense to take a course in French literature if you don't speak the language. – Evg Jul 11 '21 at 14:36
  • On a side note, this code is not handling an empty list correctly, and is not updating `headref` if the 1st node in the list is deleted. – Remy Lebeau Jul 11 '21 at 14:37
  • 1
    *... but some websites have the same code but with `free(temp)`* What websites? Maybe they had good reason to use that. Maybe their code was **C** code. Hard to say, without the links. Or better, ask someone on that website why they used `free(temp)` instead of `delete temp`. – Eljay Jul 11 '21 at 14:54
  • The Internet has no quality control. Anyone can post anything at any time, be it the truth, a partial-truth, or an outright lie with equal ease. Many don't even know they are wrong, and are just parroting something incorrect that they read/heard/saw elsewhere or sharing the results of using [undefined behaviour](https://en.cppreference.com/w/cpp/language/ub) and getting away with it. – user4581301 Jul 11 '21 at 15:43

1 Answers1

2

Which one of them is correct or better to use in C++?

Depends on where you got the pointer. If you didn't get the pointer from std::malloc (or related functions), then you must not std::free the pointer. If you didn't get the pointer from allocating non-array new, then you must not delete it. If you violate those rules, then the behaviour of the program will be undefined. It is important to avoid that.

That said, there's no good reason to use std::malloc in this case, and thus no std::free either.

P.S. I recommend against starting learning C++ by looking at random pieces of code. It's unlikely to be very efficient.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • isn't `new/delete` way proffered in C++? –  Jul 11 '21 at 14:23
  • 1
    @KPCT `new`/`delete` is preferred to`std::malloc`/`std::free`, yes. Like I said, in the answer: `there's no good reason to use std::malloc in this case, and thus no std::free either.` Using them directly isn't exactly preferrd either. Rough order of preference: 1. Use an existing implementation, standard library provides you with list containers. 2. Allocate using a templated allocator so that the user gets to choose how memory is allocated. 3. Use smart pointers. – eerorika Jul 11 '21 at 14:24