[Pyrex] Total newbie needs code review

John Machin sjmachin at lexicon.net
Mon Sep 12 03:01:07 CEST 2005


len-l at telus.net wrote:

>On 12 Sep 2005 at 9:20, Grant McDonald wrote:
>
>  
>
>>As i mentioned before the assignment of pyStuff to cStuff or the implicit function call both obtain 
>>a pointer that refers to the internal buffer of the python object not a copy, so it mustn't be 
>>modified. An example of a modifying function:
>>cdef void modifyArg(char * stuff) 
>> strcat(stuff, " and more stuff!") 
>>So if you are passing in the pointer reference as an argument to a function you must be totally 
>>sure that the function does not modify it in _any_ way. If you can't be sure malloc a char pointer 
>>to the size of the string and strcpy the contents of the pystring into it:
>>cdef char * copy 
>>pyString = "a python string!" 
>># malloc char pointer; len + 1 for null terminator 
>>copy = <char *>PyMem_Malloc(len(pyString) * sizeof(char) + 1) 
>>    
>>
>
>Should'nt this be:
>
>copy = <char *>PyMem_Malloc( (len(pyString) + 1) * sizeof(char) )
>
>if you are reserving a character's worth of space for the null terminator?
>
>
>  
>
According to K&R v2 p204, sizeof(char) == 1; pretend you are an 
optimising compiler and see what you get.




More information about the Pyrex mailing list