Skip to content

Document how to cast / recast / change type of pointer inside Ptr docs #58641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion base/pointer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
"""
Ptr{T}

A memory address referring to data of type `T`. However, there is no guarantee that the
A memory address referring to data of type `T`. However, there is no guarantee that the
memory is actually valid, or that it actually represents data of the specified type.
Some operations like [`unsafe_load`](@ref) are only supported when T is an [`isbitstype`](@ref)
while [`unsafe_wrap`](@ref) also supports abstract types.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unsafe_wrap is a good see also, but it is not an alternative to Ptr in many cases so this is not appropriate as a "while" clause.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not meant as an alternative to pointers. unsafe_wrap takes a pointer as an argument and if it is a Ptr{T} where T is an abstract type it will still worked, while unsafe_load will throw an error if given such a pointer.

Use [`reinterpret`](@ref) to get a pointer with the same address but a different type.

!!! warning
When T is a [`Union`](@ref) type, the pointer does not contain the necessary information
to reconstruct what elements in the array had which type. Such pointers can't be use to
Copy link
Member

@LilithHafner LilithHafner Jun 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pointers are not just for arrays. For example, you can get a pointer to a field in an mutable struct.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/immutable/mutable I assume

Copy link
Contributor Author

@freemin7 freemin7 Jun 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"what data type it pointed to." would that be better?

retrieve any Julia objects.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still possible to retrieve an object, you just have to know what type you're looking for.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well then you would manually de-unionize the pointer and it no longer the "same" pointer.

I will think of a different phrasing.

"""
Ptr

Expand Down