On Oct 10, 2008, at 11:20 PM, Greg Ewing wrote:

> Dag Sverre Seljebotn wrote:
>> Robert Bradshaw wrote:
>>
>>>     cdef double *a = [0.5, 0.3, 0.1, 0.1]
>>
>>> Note that it is still allocated on the stack, but a pointer type is
>>> needed as arrays aren't lvalues.
>>
>> It looks a bit confusing...
>
> I think it looks confusing, too. Is 'a' a real pointer
> here? I.e. can you assign a different pointer value to
> it later?

Yes.

> If so, then it's not obvious where the memory for the
> initial value is being allocated. A C person would
> probably guess that it's either statically allocated
> (by analogy with char *p = "literal") or heap allocated.
> Allocating it on the stack is neither a C-like nor a
> Python-like thing to do.

It's on the stack, just like literals. However, one can write

def foo(x, y):
     cdef double* a
     if x:
         a = [1,2,3,x]
     else:
         a = [1,2,3,y]

> It would be better if it were written as
>
>    cdef double a[] = [0.5, 0.3, 0.1, 0.1]
>
> the same as a statically initialized array. The fact
> that arrays aren't lvalues isn't relevant, because
> this isn't an assignment, it's a specification of
> an initial value.

Yes, I'd like to support this form as well, but it was a lot less  
"local" of a change and less flexible (see above). Also, I'd like to  
have a natural way to get memory-managed double*, etc. built into the  
language (using the allocate a string option). They would be very  
simple--contiguous one-dimensional arrays (anything more, just use  
NumPy), but would save people a lot of pain (especially those coming  
from the Python world who've never heard of malloc, memory leaks,  
segfaults ,etc.).

- Robert


_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to