Re: (Interfacing Prolog) pointer issue (SWI)



Thank You Jan. Very kind as always.
Your code works well.
But I want to submit you a "strange" behaviour of the following code that I
am unable to explain.
In particular, I implement alloci/1 and alloci/2 predicates. The first just
allocate memory (and unify pointer). The second do the job of the first, but
set also the value of the pointed address to an integer (the 2nd arg of
alloci/2). Code follows:
-------
#include <ctype.h>
#include <SWI-Prolog.h>
#include <stdlib.h>

static foreign_t
pl_alloci1(term_t address)
{
int * ptr = malloc(sizeof(int));
return (PL_unify_pointer(address, ptr));
}

static foreign_t
pl_alloci2(term_t address, term_t integer)
{
int * ptr = malloc(sizeof(int));
int i;
PL_get_integer(integer, &i);
ptr = &i;
return (PL_unify_pointer(address, ptr));
}

install_t
install()
{
PL_register_foreign("alloci1", 1, pl_alloci1, 0);
PL_register_foreign("alloci2", 2, pl_alloci2, 0);
}
-------
After load_foreign_library/1, I call:

?- alloci1(P1), alloci1(P2). % works as expected
P1 = 1069858200
P2 = 1069858290
yes
?- alloci2(P1,1), alloci2(P2,2). % now what happens?
P1 = 1069858230
P2 = 1069858230
yes

How is it possible?
Very thanks again.
/\/\


.



Relevant Pages