- 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 new[]
C++ New::operator new[] 返回一個指向第一個元素的指標,該指標在分配和初始化物件陣列後。相反,new 運算子的行為相同,但僅允許單個分配而不是陣列。使用 new 和 delete 以及 new[] 和 delete[] 進行釋放和物件刪除。
儘管 operator new[] 可以像 C++ 中的任何其他函式一樣顯式呼叫,但它具有特定的行為。如果表示式對陣列型別使用 new 運算子,它首先使用陣列型別說明符的大小作為第一個引數呼叫函式 operator new。如果成功,則會自動初始化或構建陣列中的每個物件。
語法
以下是 C++ New::operator new[] 的語法:
void* operator new[] (std::size_t size) throw (std::bad_alloc); (throwing allocation) void* operator new[] (std::size_t size, const std::nothrow_t& nothrow_value) throw(); (nothrow allocation) void* operator new[] (std::size_t size, void* ptr) throw(); (placement)
引數
- size - 包含請求的記憶體塊的大小(以位元組為單位)。
- nothrow_value - 包含常量 nothrow。
- ptr - 指向已分配的適當大小的記憶體塊的指標。
示例 1
讓我們看看下面的示例,我們將使用 operator new[] 並檢索輸出。
#include <cstdio>
#include <cstdlib>
#include <new>
void* operator new[](std::size_t sz) {
std::printf(" operator new[](size_t), size = %zu\n", sz);
if (sz == 1)
++sz;
if (void *ptr = std::malloc(sz))
return ptr;
throw std::bad_alloc{};
}
int main() {
int* p2 = new int[12];
delete[] p2;
}
輸出
讓我們編譯並執行上面的程式,這將產生以下結果:
operator new[](size_t), size = 48
示例 2
讓我們看看另一種情況,我們將使用 operator new[] 以及特定的分配。
#include <iostream>
struct A {
static void* operator new(std::size_t count) {
std::cout << "custom operator new for size " << count << '\n';
return ::operator new(count);
}
static void* operator new[](std::size_t count) {
std::cout << "custom operator new[] for size " << count << '\n';
return ::operator new[](count);
}
};
int main() {
A* p1 = new A;
delete p1;
A* p2 = new A[13];
delete[] p2;
}
輸出
執行上述程式碼後,它將顯示如下輸出:
custom operator new for size 1 custom operator new[] for size 13
示例 3
考慮以下情況,我們將檢查 operator new[] 的功能。
#include <iostream>
#include <new>
struct MyClass {
int data;
MyClass() {
std::cout << '@';
}
};
int main () {
std::cout << "constructions (1): ";
MyClass * p1 = new MyClass[10];
std::cout << '\n';
std::cout << "constructions (2): ";
MyClass * p2 = new (std::nothrow) MyClass[5];
std::cout << '\n';
delete[] p2;
delete[] p1;
return 0;
}
輸出
當代碼執行時,它將生成如下輸出:
constructions (1): @@@@@@@@@@ constructions (2): @@@@@
廣告