[C++-sig] Re: Boost::Python for C code -> Segmentation fault
Mario Palomo
mario-p at iname.com
Thu Jul 22 22:16:56 CEST 2004
thor.arne.johansen at ibas.no wrote:
>
>>typedef struct user {
>> char *name;
>> int number;
>>}user;
>>
>>void greet(user *u)
>>{
>> printf("Hello %s, your number is %d.\n"
>> , u->name, u->number);
>>}
>
>
>
> You never allocate (or initilialize) the memory for user, so your printf
> runs wild in memory.
>
> --
> Thor Arne Johansen
> Dept. Manager R&D
> Ibas AS
But I get the "Segmentation fault" and I don't use the 'greet' function (the
printf never get executed):
$ python
Python 2.3.4 (#2, Jul 5 2004, 09:15:05)
[GCC 3.3.4 (Debian 1:3.3.4-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import hello
>>> u = hello.user()
>>> u.number=10
Segmentation fault
$
I think Python allocate the memory for user, in the 'u' variable.
As a note, this same example works under SWIG, when I use this files:
==================> FILE: hello.c <==============
/*---------------- C code -------------------- */
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
typedef struct user {
char *name;
int number;
}user;
void greet(user *u)
{
printf("Hello %s, your number is %d.\n"
, u->name, u->number);
}
#ifdef __cplusplus
}
#endif
/*-------------------------------------------- */
==================> FILE: hello.i <==============
%module hello
%{
typedef struct user {
char *name;
int number;
}user;
%}
typedef struct user {
char *name;
int number;
}user;
void greet(user *u);
=================================================
Using Linux (Debian Sid) 'swig' packages, when I compile, I get:
$ swig -python hello.i
$ gcc -c hello.c -Wall
$ gcc -c hello_wrap.c -Wall -I/usr/include/python2.3
$ ld -shared -o _hello.so hello.o hello_wrap.o
I get '_hello.so' and 'hello.py', that work fine:
Python 2.3.4 (#2, Jul 5 2004, 09:15:05)
[GCC 3.3.4 (Debian 1:3.3.4-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import hello
>>> u = hello.user()
>>> u.number=10
>>> u.name="Mario"
>>> hello.greet(u)
Hello Mario, your number is 10.
>>>
Why this doesn't work on Boost::Python?
Thanks for your response.
Mario
More information about the Cplusplus-sig
mailing list