-2

So basically I want to call some C code from Prolog, and here is the code:

Prolog:

:-foreign(ptraddr_wrapper(+integer,-integer)).
:-foreign(ptrlval_wrapper(+integer,-integer)).
:-foreign(ptre_wrapper(+integer,-integer)).


% p = &b;
babelTPtr(Var, Val) :- ptraddr_wrapper(Val, Var).
% a = *p;
babelEPtr(Var, Val) :- ptre_wrapper(Val, Var).
% *p = 2;
babelLValPtr(Var, Val) :- ptrlval_wrapper(Val,Var).
% a = b;
babelAssign(Var, Val) :- Var is Val.

main :-
babelAssign(A, 1),
babelAssign(B, 2),
babelTPtr(P, A),
babelLValPtr(P, 2),
%% babelEPtr(B, P),

% print out

write(A), nl.

C:

#include </usr/local/gprolog-1.4.4/include/gprolog.h>
#include <string.h>


PlBool ptraddr_wrapper(int* ptr, int* res){
res = &ptr;
printf("%p\n", &ptr);
printf("%p\n", res);
printf("%d\n", *res);
if(res==NULL){
  return PL_FALSE;
}else{
  return PL_TRUE;
}
}

PlBool ptrlval_wrapper(int val, int* res){
   printf("%p\n", res);
   printf("%d\n", val);
   *res = val;
   printf("%d\n", *res);
if(res==NULL){
  return PL_FALSE;
}else{
  return PL_TRUE;
}
}

PlBool ptre_wrapper(int* ptr, int* res){
   *res = *ptr;
if(res==NULL){
 return PL_FALSE;
}else{
   return PL_TRUE;
   }
}

The problem is that, after I run this code, the output is :

  0xbfae8bcc       <-- it is on the stack, right
  0xbfae8bcc       <-- it is on the stack, right
   1              
  0x82abec4        <-- Why?
    2
    2

I don't understand why the fourth output is a new memory address, to my understanding, it should also be 0xbfae8bcc,

am I wrong? Could anyone give me some help?

lllllllllllll
  • 8,519
  • 9
  • 45
  • 80
  • it seems to be a C question… – Manolo Apr 03 '14 at 17:54
  • Well...I have no idea... – lllllllllllll Apr 03 '14 at 19:20
  • are you expecting 'res = &ptr;' holds after ptraddr_wrapper at the prolog level? What if you give the intuition of each function and the expected result? – Manolo Apr 03 '14 at 23:16
  • Did you try something like 'PlBool ptraddr_wrapper(int* ptr, int** res)' ? – Manolo Apr 03 '14 at 23:18
  • @Manolo Hi, thank you for your reply, so my intuition is to simulate three kinds of pointer usage scenarios using prolog+C(you can find my comment in the first code snippet) – lllllllllllll Apr 03 '14 at 23:23
  • @Manolo Yes, I tried "PlBool ptraddr_wrapper(int* ptr, int** res)", but I still don't know how to implement "PlBool ptrlval_wrapper" – lllllllllllll Apr 03 '14 at 23:24
  • I'll compile and run your code next monday. I doubt the memory address is modifiable at prolog level. You should try to contact Daniel Diaz... He use to answer emails about gprolog. – Manolo Apr 04 '14 at 06:53

1 Answers1

2

What you try to do is simply impossible (it would be very dangerous). In the C part, the res pointer is the address of a temporary variable which, at the return of the function, should contain a result. This result is unified with the corresponding Prolog argument bu the foreign interface. You can look at the global variables facilities offered by gprolog, in particular g_link/2. see doc

didou
  • 692
  • 3
  • 7
  • Hi didou, thank you a lot for your answer! I am not quite familiar with the internal concept of Prolog, and I just don't understand why you let look at g_link/2, so you mean my current approach is impossible but I can somehow achieve this goal by using g_link/2..? COuld you please give me more details on this issue, because it is really important to me...Thank you! – lllllllllllll Apr 12 '14 at 05:52