users-prolog
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: access list elements from C code


From: Daniel Diaz
Subject: Re: access list elements from C code
Date: Wed, 27 Jun 2012 15:15:12 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20120430 Thunderbird/12.0.1

Le 23/06/2012 13:57, dmaugis a écrit :
Well...I m trying to access list element from C code, and obviously there is
something I miss-understood, because I crash. any help/explanation would be
welcome.

On the gprolog side I have:

:-foreign(sdl_init(+term)).

initialise :-
                sdl_init([timer,audio,video]).

On the C side:

int             sdl_init(PlTerm list)
{
        unsigned long flags=0;
        if(Pl_Builtin_List(list)) {
                PlTerm *terms=NULL;
                int len=Pl_List_Length(list);
                terms=malloc(sizeof(PlTerm)*len);
                if(terms) {
                        unsigned i=Pl_Rd_Proper_List_Check(list, terms);
                        for(i=0; i<len; i++) {
                                char* s;
                                switch(Pl_Type_Of_Term(terms[i])) {
                                case PL_INT:
                                        printf("integer.\n");
                                        flags|=Pl_Rd_Integer(terms[i]);
                                        break;
                                case PL_ATM:
                                        s=Pl_Atom_Name(terms[i]);
                                        printf("atom '%s'\n",Pl_Is_Valid_Atom(terms[i]) ? 
"valid":"invalid");
                                        break;
                                }
                        }
                        
And I get:

| ?- go.
atom 'invalid'
Fatal Error: Segmentation Violation

it seems when doing :

s=Pl_Atom_Name(terms[i]);



Your code is incomplete and cannot be compiled... Next time, please provide a usable code...

However there is clearly an error here:

s=Pl_Atom_Name(terms[i]);
printf("atom '%s'\n",Pl_Is_Valid_Atom(terms[i]) ? "valid":"invalid");

Because you don't "obtain" the atom. Use Pl_Rd_Atom() for this. Example:

int atom = Pl_Rd_Atom(terms[i]);
s=Pl_Atom_Name(atom);
printf("atom %s is '%s'\n",s,Pl_Is_Valid_Atom(Pl_Rd_Atom(terms[i])) ? "valid":"invalid");

Daniel


--
Ce message a ete verifie par MailScanner
pour des virus ou des polluriels et rien de
suspect n'a ete trouve.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]