ctypes - python2.7.3 vs python3.2.3

MRAB python at mrabarnett.plus.com
Tue Aug 28 17:59:16 EDT 2012


On 28/08/2012 22:35, Rolf wrote:
> ctypes works as I would expect with python2.7.3.
>
> However, when I upgrade to python3.2.3 things don't seem to work right. Look below for details.
>
> I am not sure where I am going wrong.
>
> Shared Library
> ==============
> #include <stdint.h>
> #include <string.h>
>
> extern "C"
> {
>     int main();
>     uint32_t myfunction (char **);
> }
>
> uint32_t myfunction (char ** _mydata)
> {
>     char mydata[16];
>
>     strcpy(mydata, "Hello Dude!");
>
>     *_mydata = mydata;
>
>     return 0;
> }
>
> int main()
> {
>     return 0;
> }
>
[snip]
What you're doing in 'myfunction' looks wrong to start with. It's
returning the address of the local array 'mydata' which allocated on
the stack when the function is entered. When the function is left it's
deallocated, so the address becomes a dangling pointer. That it gave a
reasonable result with Python 2.7.3 is down to pure luck.



More information about the Python-list mailing list