本文簡要介紹rust語言中 Function alloc::alloc::alloc 的用法。
用法
pub unsafe fn alloc(layout: Layout) -> *mut u8使用全局分配器分配內存。
此函數將調用轉發到使用 #[global_allocator] 屬性注冊的分配器的  GlobalAlloc::alloc  方法(如果有的話),或者 std 板條箱的默認值。
當它和 Allocator  trait 變得穩定時,預計此函數將被棄用,取而代之的是 Global  類型的alloc 方法。
安全性
例子
use std::alloc::{alloc, dealloc, Layout};
unsafe {
    let layout = Layout::new::<u16>();
    let ptr = alloc(layout);
    *(ptr as *mut u16) = 42;
    assert_eq!(*(ptr as *mut u16), 42);
    dealloc(ptr, layout);
}相關用法
- Rust alloc_zeroed用法及代碼示例
- Rust align_of用法及代碼示例
- Rust always_abort用法及代碼示例
- Rust align_of_val用法及代碼示例
- Rust align_of_val_raw用法及代碼示例
- Rust assert_ne用法及代碼示例
- Rust array.map用法及代碼示例
- Rust assert_matches用法及代碼示例
- Rust addr_of_mut用法及代碼示例
- Rust array.split_array_ref用法及代碼示例
- Rust array用法及代碼示例
- Rust array.zip用法及代碼示例
- Rust assert_eq用法及代碼示例
- Rust array.split_array_mut用法及代碼示例
- Rust available_parallelism用法及代碼示例
- Rust addr_of用法及代碼示例
- Rust abort用法及代碼示例
- Rust array.each_mut用法及代碼示例
- Rust assert用法及代碼示例
- Rust array.each_ref用法及代碼示例
- Rust args用法及代碼示例
- Rust args_os用法及代碼示例
- Rust UdpSocket.set_multicast_loop_v6用法及代碼示例
- Rust i64.overflowing_add_unsigned用法及代碼示例
- Rust Box.downcast用法及代碼示例
注:本文由純淨天空篩選整理自rust-lang.org大神的英文原創作品 Function alloc::alloc::alloc。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
