
- C標準庫
- C標準庫
- C++標準庫
- C++庫 - 首頁
- C++庫 - <fstream>
- C++庫 - <iomanip>
- C++庫 - <ios>
- C++庫 - <iosfwd>
- C++庫 - <iostream>
- C++庫 - <istream>
- C++庫 - <ostream>
- C++庫 - <sstream>
- C++庫 - <streambuf>
- C++庫 - <atomic>
- C++庫 - <complex>
- C++庫 - <exception>
- C++庫 - <functional>
- C++庫 - <limits>
- C++庫 - <locale>
- C++庫 - <memory>
- C++庫 - <new>
- C++庫 - <numeric>
- C++庫 - <regex>
- C++庫 - <stdexcept>
- C++庫 - <string>
- C++庫 - <thread>
- C++庫 - <tuple>
- C++庫 - <typeinfo>
- C++庫 - <utility>
- C++庫 - <valarray>
- C++ STL庫
- C++庫 - <array>
- C++庫 - <bitset>
- C++庫 - <deque>
- C++庫 - <forward_list>
- C++庫 - <list>
- C++庫 - <map>
- C++庫 - <multimap>
- C++庫 - <queue>
- C++庫 - <priority_queue>
- C++庫 - <set>
- C++庫 - <stack>
- C++庫 - <unordered_map>
- C++庫 - <unordered_set>
- C++庫 - <vector>
- C++庫 - <algorithm>
- C++庫 - <iterator>
- C++高階庫
- C++庫 - <any>
- C++庫 - <barrier>
- C++庫 - <bit>
- C++庫 - <chrono>
- C++庫 - <cinttypes>
- C++庫 - <clocale>
- C++庫 - <condition_variable>
- C++庫 - <coroutine>
- C++庫 - <cstdlib>
- C++庫 - <cstring>
- C++庫 - <cuchar>
- C++庫 - <charconv>
- C++庫 - <cfenv>
- C++庫 - <cmath>
- C++庫 - <ccomplex>
- C++庫 - <expected>
- C++庫 - <format>
- C++庫 - <future>
- C++庫 - <flat_set>
- C++庫 - <flat_map>
- C++庫 - <filesystem>
- C++庫 - <generator>
- C++庫 - <initializer_list>
- C++庫 - <latch>
- C++庫 - <memory_resource>
- C++庫 - <mutex>
- C++庫 - <mdspan>
- C++庫 - <optional>
- C++庫 - <print>
- C++庫 - <ratio>
- C++庫 - <scoped_allocator>
- C++庫 - <semaphore>
- C++庫 - <source_location>
- C++庫 - <span>
- C++庫 - <spanstream>
- C++庫 - <stacktrace>
- C++庫 - <stop_token>
- C++庫 - <syncstream>
- C++庫 - <system_error>
- C++庫 - <string_view>
- C++庫 - <stdatomic>
- C++庫 - <variant>
- C++ STL庫速查表
- C++ STL - 速查表
C++ New::operator delete
C++ New::operator delete是一個常規函式,可以像其他任何函式一樣顯式呼叫。但是,在C++中,delete是一個具有非常特定行為的運算子:包含delete運算子的表示式首先呼叫正確的解構函式(對於類型別),然後呼叫釋放函式。
如果存在,名為operator delete的成員函式將作為類物件的釋放方法。在所有其他情況下,它是一個全域性函式運算子,稱為delete。如果delete語句前面帶有作用域運算子,則只考慮全域性釋放函式。
語法
以下是C++ New::operator delete的語法:
void operator delete (void* ptr) throw(); (ordinary delete) void operator delete (void* ptr, const std::nothrow_t& nothrow_constant) throw(); (nothrow delete) void operator delete (void* ptr, void* voidptr2) throw(); (placement delete)
引數
- size − 包含請求的記憶體塊的大小(以位元組為單位)。
- nothrow_value − 包含常量nothrow。
- ptr − 指向已分配的適當大小的記憶體塊的指標。
- voidptr2 − 一個void指標。
示例1
讓我們來看下面的例子,我們將使用operator delete並獲取輸出。
#include <cstdio> #include <cstdlib> #include <new> void operator delete(void* ptr, std::size_t size) noexcept { std::printf("delete(void*, size_t), size = %zu\n", size); std::free(ptr); } int main() { int* p1 = new int; delete p1; }
輸出
讓我們編譯並執行上面的程式,這將產生以下結果:
delete(void*, size_t), size = 4
示例2
讓我們來看另一個場景,我們將使用operator delete並使用delete和delete[]獲取輸出。
#include <iostream> struct a { static void operator delete(void* ptr, std::size_t sz) { std::cout << "operator delete: " << sz << '\n'; ::operator delete(ptr); } static void operator delete[](void* ptr, std::size_t sz) { std::cout << "operator delete[]: " << sz << '\n'; ::operator delete[](ptr); } }; int main() { a* p1 = new a; delete p1; a* p2 = new a[12]; delete[] p2; }
輸出
執行上面的程式碼後,它將顯示如下輸出:
operator delete: 1 operator delete[]: 20
示例3
考慮以下情況,我們將檢查operator delete的功能。
#include <iostream> struct MyClass { MyClass() {std::cout <<"It is a MyClass() constructed\n";} ~MyClass() {std::cout <<"It is a MyClass() destroyed\n";} }; int main () { MyClass * pt = new (std::nothrow) MyClass; delete pt; return 0; }
輸出
程式碼執行後,將生成如下輸出:
It is a MyClass() constructed It is a MyClass() destroyed
廣告