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.
C++11 =delete Specifier
Prior to C++11, the
delete
operator was solely used to deallocate memory that was dynamically allocated using the
new
operator. With C++11, the
=delete
specifier was introduced to explicitly disable (or delete) the usage of a member function, preventing it from being called or generated by the compiler.
Key Points about =delete
- Purpose: The
=delete
specifier is used to explicitly mark a function as deleted, meaning it cannot be used or invoked. Any attempt to call a deleted function results in a compilation error.
- Syntax: It is appended to a function declaration, e.g.,
void someFunction() = delete;
.
- Common Use Cases:
- Disabling Implicitly Declared Functions: It is often used to suppress compiler-generated special member functions, such as the default constructor, copy constructor, copy assignment operator, or destructor. For example:
class MyClass {
public:
MyClass(const MyClass&) = delete; // Disable copy constructor
MyClass& operator=(const MyClass&) = delete; // Disable copy assignment
};
This ensures that objects of MyClass
cannot be copied.
- Preventing Specific Overloads: It can be used to disable specific function overloads or template specializations. For example:
void process(int) {}
void process(double) = delete; // Disallow calling with double
- Restricting Object Creation: For instance, to prevent stack allocation by deleting the default constructor:
class MyClass {
public:
MyClass() = delete; // Prevent default construction
};
- Explicitly Deleted Function: A function marked with
=delete
is called an explicitly deleted function. It participates in overload resolution but causes a compilation error if selected.
- Not Limited to Special Member Functions: While commonly used for implicit or special member functions,
=delete
can be applied to any function, including user-defined member functions, non-member functions, or even free functions.
Why Use =delete
?
- Improved Code Safety: It allows developers to explicitly prevent undesirable operations, making the code's intent clearer and catching errors at compile time.
- Better Alternative to Private Functions: Before C++11, disabling a function often involved declaring it
private
and leaving it undefined, which was less clear and could lead to linker errors. =delete
is a cleaner, compile-time solution.
- Control Over Implicit Behavior: It gives fine-grained control over which operations are allowed, especially for classes with specific design constraints (e.g., non-copyable classes like
std::unique_ptr
).
Example in Action
#include <iostream>
class NonCopyable {
public:
NonCopyable() = default;
NonCopyable(const NonCopyable&) = delete; // Disable copy constructor
NonCopyable& operator=(const NonCopyable&) = delete; // Disable copy assignment
};
int main() {
NonCopyable obj1;
// NonCopyable obj2 = obj1; // Compilation error: copy constructor is deleted
// obj1 = NonCopyable(); // Compilation error: copy assignment is deleted
return 0;
}
In this example, attempts to copy
obj1
will fail at compile time because the copy constructor and assignment operator are explicitly deleted. If you have further questions about
=delete
or its applications, feel free to ask!

