struct Slice(T)

Overview

A Slice is a Pointer with an associated size.

While a pointer is unsafe because no bound checks are performed when reading from and writing to it, reading from and writing to a slice involve bound checks. In this way, a slice is a safe alternative to Pointer.

A Slice can be created as read-only: trying to write to it will raise. For example the slice of bytes returned by String#to_slice is read-only.

Included Modules

Defined in:

slice.cr

Constructors

Constructor Detail

def self.new(pointer : ::Pointer(::Pointer(U)), *, limit : Int, read_only = false) : Slice(::Pointer(U)) forall U #

Creates a Slice to the given pointer, bounded by a null-terminator, copying at most limit elements.

The use of a 'reasonable' value for limit is strongly recommended. This will prevent the function from unlimited reading of unintended memory.

ptr = LibC.getgrnam("wheel").value.gr_mem
slice = Slice.new(ptr, limit: LibC::NGROUP_MAX, read_only: true)

[View source]