[Pyrex] Total newbie needs code review

len-l at telus.net len-l at telus.net
Mon Sep 12 02:51:20 CEST 2005


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?

Lenard Lindstrom
<len-l at telus.net>




More information about the Pyrex mailing list