← 输入框|github.com|刷新
Skip to content

Add a test for taking a CPtr pointer of a struct (dataclass) from Python #1787

Open

Description

@certik

Using the p_c_pointer approach.

Activity

  1. certik commented on May 11, 2023

    @certik
    ContributorAuthor

    Currently the only way seems to be to allocate the memory in C and then use as follows:

            sptr: CPtr = my_allocate(sizeof(S))
            s: Pointer[S] = c_p_pointer(sptr, S)
            s.k = 5

    But we would like something like:

    s: S = S()
    s.k = 5
    sptr: CPtr = p_c_pointer(s)
  2. czgdp1807 commented on May 16, 2023

    @czgdp1807
    Collaborator

    Note the differences.

    sptr: CPtr = my_allocate(sizeof(S))
            s: Pointer[S] = c_p_pointer(sptr, S)
            s.k = 5

    This would do a call to C function my_allocate which further calls malloc and then returns a pointer which gets assigned to sptr.

    s: S = S()
    s.k = 5
    sptr: CPtr = p_c_pointer(s)

    Here S gets created using alloca LLVM instruction then it is bit casted to void* and finally assigned to sptr.

    @certik I think the first one already works. If you think the second one that is creating struct using LLVM's alloca is correct behaviour then I will go ahead with that.

  3. self-assigned this
    on May 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions