Skip to content

Use &raw to get address of globals#77

Merged
nunoplopes merged 2 commits into
Cpp2Rust:masterfrom
lucic71:addr-of-global
May 12, 2026
Merged

Use &raw to get address of globals#77
nunoplopes merged 2 commits into
Cpp2Rust:masterfrom
lucic71:addr-of-global

Conversation

@lucic71
Copy link
Copy Markdown
Contributor

@lucic71 lucic71 commented May 11, 2026

Globals are translated as static mut. Taking the address of globals using &T as *const T is unsafe and not allowed in const initializers. &T creates a reference to the static mut, and the Rust compiler cannot prove the soudness of this operation.

To overcome this, use &raw const T instead of &T as &const T. &raw simply computes the address without creating an immutable reference.

@nunoplopes nunoplopes merged commit 5d052ba into Cpp2Rust:master May 12, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants