Skip to main content

dealloc

Function dealloc 

1.36.0 · Source
pub unsafe fn dealloc(ptr: *mut u8, layout: Layout)
Expand description

Deallocates memory with the global allocator.

This function forwards calls to the GlobalAlloc::dealloc method of the allocator registered with the #[global_allocator] attribute if there is one, or the std crate’s default.

Note, however, that invoking this function is not equivalent to invoking the underlying GlobalAlloc::dealloc method of the registered allocator directly. Users of this function cannot assume anything about what the allocator does, other than the documented requirements. This means:

  • This function may non-deterministically entirely skip the underlying allocator, e.g. if the compiler can show that this allocation can be replaced by a stack variable. The compiler may also merge multiple allocation operations into one, as long as it can also adjust all corresponding deallocation operations accordingly.
  • The pointer passed to this function must have been obtained by invoking alloc, alloc_zeroed, or realloc. In particular, passing a pointer returned by the underlying methods on GlobalAlloc is not permitted.
  • This function de-initializes the contents of the allocation before handing it to the allocator. So even if you know that the program previously initialized that memory, the allocator cannot rely on it being initialized.

Users of this function have to consider that in the future, allocators may be allowed to unwind.

This function is expected to be deprecated in favor of the deallocate method of the Global type when it and the Allocator trait become stable.

§Safety

See GlobalAlloc::dealloc.