C++ åèæå
- C++11
- C++14
- C++17
- C++20
- C++ ç¼è¯å¨æ¯ææ åµè¡¨
- ç¬ç«ä¸å®¿ä¸»å®ç°
- C++ è¯è¨
- C++ å ³é®è¯
- é¢å¤çå¨
- C++ æ ååºå¤´æä»¶
- å ·åè¦æ±
- åè½ç¹æ§æµè¯ (C++20)
- å·¥å ·åº
- ç±»åæ¯æï¼åºæ¬ç±»åãRTTIãç±»åç¹æ§ï¼
- æ¦å¿µåº (C++20)
- é误å¤ç
- 卿å å管ç
- std::addressof
- std::allocator_traits
- std::unique_ptr
- std::scoped_allocator_adaptor
- std::auto_ptr
- std::destroy_at
- std::destroy
- std::destroy_n
- std::uninitialized_move
- std::uninitialized_value_construct
- std::owner_less
- std::shared_ptr
- std::to_address
- std::assume_aligned
- std::make_obj_using_allocator
- C å å管çåº
- ä½å±å å管ç
- std::pmr::memory_resource
- std::allocator
- std::allocator<T>::construct
- std::allocator<T>::destroy
- std::allocator<T>::allocate
- std::default_delete
- std::allocator_arg_t
- std::allocator_arg
- std::weak_ptr
- std::enable_shared_from_this
- std::bad_weak_ptr
- std::pointer_traits
- std::uses_allocator
- std::uses_allocator_construction_args
- std::uninitialized_construct_using_allocator
- std::pmr::polymorphic_allocator
- std::pmr::get_default_resource
- std::pmr::set_default_resource
- std::pmr::new_delete_resource
- std::pmr::null_memory_resource
- std::pmr::synchronized_pool_resource
- std::pmr::unsynchronized_pool_resource
- std::pmr::monotonic_buffer_resource
- std::pmr::pool_options
- std::raw_storage_iterator
- std::get_temporary_buffer
- std::return_temporary_buffer
- std::uninitialized_copy
- std::uninitialized_fill
- std::uninitialized_default_construct
- std::uninitialized_copy_n
- std::uninitialized_fill_n
- std::uninitialized_move_n
- std::uninitialized_default_construct_n
- std::uninitialized_value_construct_n
- std::construct_at
- std::align
- 注é
- æ¥æåæ¶é´å·¥å ·
- å符串åº
- 容å¨åº
- è¿ä»£å¨åº
- èå´åº (C++20)
- ç®æ³åº
- æ°å¼åº
- è¾å ¥/è¾åºåº
- æä»¶ç³»ç»åº
- æ¬å°ååº
- æ£å表达å¼åº
- ååæä½åº
- çº¿ç¨æ¯æåº
- å®éªæ§ C++ ç¹æ§
- æç¨çèµæº
- ç´¢å¼
- std 符å·ç´¢å¼
- åç¨æ¯æ (C++20)
- C++ å ³é®è¯
ä½ç½®ï¼é¦é¡µ > C++ åèæå >卿å å管ç >std::allocator > std::allocator<T>::allocate
std::allocator<T>::allocate
| (1) | ||
| pointer allocate( size_type n, const void * hint = 0 ); |
(C++17 å) | |
| T* allocate( std::size_t n, const void * hint); |
(C++17 èµ·) (å¼ç¨) (C++20 ä¸ç§»é¤) |
|
| (2) | ||
| T* allocate( std::size_t n ); |
(C++17 èµ·) (C++20 å) |
|
| [[nodiscard]] constexpr T* allocate( std::size_t n ); |
(C++20 èµ·) | |
è°ç¨ ::operator new(std::size_t) æ ::operator new(std::size_t, std::align_val_t) (C++17 èµ·)åé
n * sizeof(T) åèçæªåå§ååå¨ï¼ä½ä½æ¶åå¦ä½è°ç¨æ¤å½æ°æ¯æªæå®çãæé hint å¯ç¨äºæä¾å¼ç¨çå±é¨æ§ï¼è¥å®ç°æ¯æï¼å allocator ä¼è¯å¾åé
å°½å¯è½æ¥è¿ hint çæ°å
ååã
è¥ T 为ä¸å®æ´ç±»å忤彿°ç使ç¨ä¸ºè°¬æã
|
为äºå¨å¸¸é表达å¼ä¸ä½¿ç¨æ¤å½æ°ï¼å¿ é¡»å¨åä¸è¡¨è¾¾å¼çæ±å¼å è§£åé å ¶æåé çåå¨ã |
(C++20 èµ·) |
åæ°
| n | - | è¦åé åå¨çå¯¹è±¡æ° |
| hint | - | æå临è¿å åä½ç½®çæé |
è¿åå¼
æåéå½å¯¹é½å¹¶è¶³ä»¥ä¿æ T ç±»åç n 个对象æ°ç»çå
ååé¦åèçæéã
å¼å¸¸
|
è¥ std::numeric_limits<std::size_t>::max() / sizeof(T) < n åæåº std::bad_array_new_length ã |
(C++11 èµ·) |
è¥åé 失败åæåº std::bad_alloc ã
注解
é£è¯âæªæå®ä½æ¶åå¦ä½â令æ ååºå®¹å¨å¯ä»¥ç»åæä¼åæå åé
ï¼å³ä½¿å¯¹ç´æ¥è°ç¨ ::operator new ç¦æ¢è¿ç§ä¼åãä¾å¦ libc++ å®ç°äºå®ï¼ [1] ä¸ [2] ï¼
ç¼ºé·æ¥å
ä¸åæ´æ¹è¡ä¸ºçç¼ºé·æ¥å追溯å°åºç¨äºä»¥ååºçç C++ æ åã
| DR | åºç¨äº | åºçæ¶çè¡ä¸º | æ£ç¡®è¡ä¸º |
|---|---|---|---|
| LWG 3190 | C++11 | allocate å¯è½åé
大å°é误çåå¨
|
æ¿èæåº bad_array_new_length
|
åé
| [éæ] |
ç¨åé
å¨åé
æªåå§åçåå¨ ( std::allocator_traits<Alloc> çå
¬å¼éææå彿°) |