Lesson 6 | A singly linked list |
Objective | The del() member function |
C++ del() function
C++ doesn't have a `del()` function like you might find in Python. However, it does have keywords and operators that are used for memory management:
- `delete` keyword: The `delete` keyword is used to deallocate memory that was dynamically allocated using the `new` operator. It works specifically with pointers:
int* ptr = new int(10); // Allocate memory for an integer
// ... use the value in ptr
delete ptr; // Deallocate the memory
- `delete[]` keyword: This is a variant of `delete` used specifically for deallocating memory that was allocated for arrays using `new[]`:
int* arr = new int[5]; // Allocate an array of 5 integers
// ...use the array
delete[] arr; // Deallocate the array
Important Notes:
- Calling destructors: When you use `delete` or `delete[]`, the objects' destructors are automatically called before the memory is actually freed. This helps manage resources correctly.
- Memory leaks and undefined behavior: Failing to use `delete` on dynamically allocated memory leads to memory leaks. Using `delete` twice on the same memory block or using `delete` on non-dynamically allocated memory causes undefined behavior.
Examine the del() member function of the singly linked list implementation.
The member function del
has the inverse role of the prepend
member function.
void slist::del(){
slistelem* temp = h;
h = h -> next; //presumes non-empty slist
delete temp;
}
The del()
member function returns the first element of the list to free store. It does this by using the delete
operator on the head of slist
pointer h
. The new head of list is the value of the next
member. Much of list processing is repetitively chaining down the list until the null pointer value is found. To conclude our examination of the
singly linked list implementation, let's take a look at the print()
and release()
member functions which use this technique.
Deleted Function
Prior to C++ 11, the operator delete had only one purpose, to deallocate a memory that has been allocated dynamically.
The C++ 11 standard introduced another use of this operator, which is: To disable the usage of a member function. This is done by appending the =delete; specifier to the end of that function declaration.
Any member function whose usage has been disabled by using the ‘=delete’ specifier is known as an explicitly deleted function.
Although not limited to them, but this is usually done to implicit functions. The following examples exhibit some of the tasks where this feature comes handy:
Disabling copy constructors
/* C++ program to disable the usage of copy-constructor using delete operator */
#include <iostream>
using namespace std;
class A {
public:
A(int x): m(x)
{
}
// Delete the copy constructor
A(const A&) = delete;
// Delete the copy assignment operator
A& operator=(const A&) = delete;
int m;
};
int main()
{
A a1(1), a2(2), a3(3);
// Error, the usage of the copy
// assignment operator is disabled
a1 = a2;
// Error, the usage of the
// copy constructor is disabled
a3 = A(a2);
return 0;
}