access C pointer to start of array

45 views
Skip to first unread message

jsr

unread,
Jun 3, 2021, 4:33:51 PM6/3/21
to cython-users
hi,

I'd like to know if its possible to access the C adress of an array declared as "double[:] ndx"
instead of using "&ndx[0]", which wont work if the size is null (because its first element doesnt exist).
I want to call a C function with it, so it has to be a C pointer, not a Python object.

cdef int bar(double[:] ndx, int n) except -1:
    # here &ndx[0] fails with "IndexError: Out of bounds on buffer access (axis 0)" when size==0 (obviously)
    # is it possible to access a valid C pointer of the start of the memoryviewslice ndx instead ?
    c_function(&ndx[0], n)
    return 0

I put the whole example on github making it as minimal as possible in case someone wants to check it out:


Robert Bradshaw

unread,
Jun 3, 2021, 9:19:52 PM6/3/21
to cython...@googlegroups.com
A memory view like double[:] may not be contiguous, which wouldn't have a direct pointer representation. If the size is zero, it's not required that there be any pointer to get at all. If you know it's contiguous, you could do (nullptr if ndx.shape[0] == 0 else &ndx[0]).

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cython-users/f676da31-1269-4675-8a17-8dfbf37e9a03n%40googlegroups.com.

jsr

unread,
Jun 4, 2021, 10:12:46 AM6/4/21
to cython-users
It should be continuous, so I was hoping for something equivalent to c++11''s std::vector::data() which gives a valid pointer to the begin of the array even when empty.


Robert Bradshaw

unread,
Jun 4, 2021, 2:14:36 PM6/4/21
to cython...@googlegroups.com
You should be able to write such a function, e.g.

cdef double* data_ptr(double[::1] view):
  if view.shape[0] == 0:
    return nullptr  # or take the address of a global double
  else:
    return &view[0]


On Fri, Jun 4, 2021 at 7:12 AM jsr <julien.s...@gmail.com> wrote:
It should be continuous, so I was hoping for something equivalent to c++11''s std::vector::data() which gives a valid pointer to the begin of the array even when empty.


--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.

D Woods

unread,
Jun 4, 2021, 3:02:05 PM6/4/21
to cython-users
If it's contiguous then you should declare it as double[::1] so enforce/document that.

I believe a memoryview slice should have a data attribute. it looks to be typed as char* so you'll have to cast it, but that's probably what you're after.

Robert Bradshaw

unread,
Jun 4, 2021, 3:48:05 PM6/4/21
to cython...@googlegroups.com
On Fri, Jun 4, 2021 at 12:02 PM D Woods <dw-...@d-woods.co.uk> wrote:
If it's contiguous then you should declare it as double[::1] so enforce/document that.

+1
 
I believe a memoryview slice should have a data attribute. it looks to be typed as char* so you'll have to cast it, but that's probably what you're after.

If this handles offsets correctly in the case of slicing (I don't remember), that could be a good option. 
 


On Friday, June 4, 2021 at 3:12:46 PM UTC+1 jsr wrote:
It should be continuous, so I was hoping for something equivalent to c++11''s std::vector::data() which gives a valid pointer to the begin of the array even when empty.


--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.

jsr

unread,
Jun 7, 2021, 2:06:27 AM6/7/21
to cython-users
On Friday, June 4, 2021 at 9:48:05 PM UTC+2 robe...@gmail.com wrote:
On Fri, Jun 4, 2021 at 12:02 PM D Woods <dw-...@d-woods.co.uk> wrote:
If it's contiguous then you should declare it as double[::1] so enforce/document that.

+1
 
I believe a memoryview slice should have a data attribute. it looks to be typed as char* so you'll have to cast it, but that's probably what you're after.

If this handles offsets correctly in the case of slicing (I don't remember), that could be a good option.

I tried "<double*>ndx.data" without luck:
c_function(<double*>ndx.data, n)
num.pyx:21:15: Casting temporary Python object to non-numeric non-Python type
 
and print(ndx.data) raises AttributeError, so I'm out of luck there
Reply all
Reply to author
Forward
0 new messages