[C++-sig] Re: Boost::Python for C code -> Segmentation fault

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Thu Jul 22 23:13:40 CEST 2004


I am somewhat surprised that your code compiles. While you are right that
Boost.Python manages the memory for the user instance, it certainly doesn't
manage your char* name. I cannot explain how exactly the segmentation fault
comes about, but you certainly have to do something about the raw char*. As a
start I'd remove the extern "C" declaration and replace char* name by
std::string name. Does that make it work as expected?
Ralf

--- Mario Palomo <mario-p at iname.com> wrote:
> 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
> 
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
> 



		
__________________________________
Do you Yahoo!?
Read only the mail you want - Yahoo! Mail SpamGuard.
http://promotions.yahoo.com/new_mail 



More information about the Cplusplus-sig mailing list