unsafe_load
unsafe_load(p::Ptr{T},i::Integer)
Load a value of type T from the address of the ith element (1-indexed) starting at p. This is equivalent to the C expression p[i-1].
The unsafe prefix on this function indicates that no validation is performed on the pointer p to ensure that it is valid. Incorrect usage may segfault your program or return garbage answers, in the same manner as C.
Examples
# Load a value from a pointer
julia> ptr = pointer([10, 20, 30]);
julia> unsafe_load(ptr, 2)
20
# Accessing elements from a pointer to a struct
julia> struct Point
x::Float64
y::Float64
end
julia> p = pointer(Point(2.0, 3.5));
julia> unsafe_load(p, 1).x
2.0
julia> unsafe_load(p, 1).y
3.5
In the first example, we use unsafe_load to load the value at the second element (index 2) of the pointer ptr. It returns the value 20.
In the second example, we define a struct Point and create a pointer p to an instance of Point. We then use unsafe_load to access the fields x and y of the struct at index 1 (the only element). It returns the respective values 2.0 and 3.5.
Note that unsafe_load is an unsafe function, and it does not perform any validation on the pointer p. It is crucial to ensure that the pointer is valid and points to the expected memory location to avoid program crashes or incorrect results.
See Also
assert, backtrace, code_llvm, code_lowered, code_native, code_typed, code_warntype, :@which, compilecache, current_module, eval, finalize, finalizer, fullname, function_module, function_name, include_dependency, InterruptException, invoke, isconst, isdefined, isgeneric, methodswith, method_exists, module_name, module_parent, require, subtypes, unsafe_load, workspace, __precompile__,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.