Skip to content

Manipulating C pointers #546

Closed
Closed

Description

@certik

Here is an example of how pointers should be manipulated, in Fortran, Python, both should generate the same ASR, and the C backend should then generate the output below:

Fortran:

type(c_ptr) :: queries
integer(2), pointer :: x(:)         
c_f_pointer(queries, x)
c_loc(x(idx))       

Python:

from ltypes import c_loc, c_p_pointer, CPtr, Pointer
from typing import Annotate
queries: CPtr
x: Annotate[i16[:], Pointer]
x = c_p_pointer(queries, i16)                      
c_loc(x[idx])           

C output:

void *queries;
int16_t *x;
x = (int16_t*)queries;
&x[idx];

Also allow passing void * pointers as arguments to functions.

Activity

  1. certik commented on Jun 1, 2022

    @certik
    ContributorAuthor

    To pass void * as function arguments, in Python:

    @ccaller
    def f(A_void: CPtr, idx: i32) -> None:
        A: Annotate[i16[:], Pointer]   
        A = c_p_pointer(A_void, i16)
        g(c_loc(A[idx]))

    translates to C:

    void f(void *A_void, int32_t idx) {
        int16_t *A;
        A = (int16_t *) A_void;
        g(&A[idx])
  2. certik commented on Jun 1, 2022

    @certik
    ContributorAuthor

    Note: I think c_loc(x[idx]) will not work in CPython, since x[idx] will return an integer as a value, so we can't take pointer to it from inside c_loc. We'll have to use some syntax from https://docs.python.org/3/library/ctypes.html#pointers.

  3. czgdp1807 commented on Jun 7, 2022

    @czgdp1807
    Collaborator

    In Python, instead of x = c_p_pointer(queries, i16) can we do, c_p_pointer(queries, x)? @certik

  4. certik commented on Jun 8, 2022

    @certik
    ContributorAuthor

    Yes, that's fine, if it can be done.

  5. certik commented on Jun 15, 2022

    @certik
    ContributorAuthor

    Most of these are fixed. The remaining issue is #609.

  6. ubaidsk commented on Sep 9, 2023

    @ubaidsk
    Collaborator

    can we do, c_p_pointer(queries, x)

    Can this be done @certik , @czgdp1807 ? Probably not. I think Python does not allow/support updating a variable passed as argument.

  7. certik commented on Sep 10, 2023

    @certik
    ContributorAuthor

    Right, Python does not support it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    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