Skip to content
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

Allow weak pointers pointing to stack or global storage #279

Open
vtereshkov opened this issue Apr 27, 2023 · 0 comments
Open

Allow weak pointers pointing to stack or global storage #279

vtereshkov opened this issue Apr 27, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@vtereshkov
Copy link
Owner

vtereshkov commented Apr 27, 2023

Weak pointers are now only allowed for heap-allocated data:

fn f() {
        a := 42
        wp := weak ^int(&a)
        if p := ^int(wp); p != null {
                printf(repr(p^) + '\n')
        } else {
                printf("NULL\n")
        }
}

fn g() {
        q := new(int, 42)
        wp := weak ^int(q)
        if p := ^int(wp); p != null {
                printf(repr(p^) + '\n')
        } else {
                printf("NULL\n")
        }
}

fn main() {
        f()        // NULL 
        g()        // 42
}

From the language specification:

If the weak pointer does not point to a valid dynamically allocated memory block, the result of this conversion is null.

Related issue: #274

@vtereshkov vtereshkov added the enhancement New feature or request label Apr 27, 2023
@vtereshkov vtereshkov changed the title Allow weak pointers to stack or global storage Allow weak pointers pointing to stack or global storage Aug 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant