Is it possible to get these memory addresses in gnu prolog?

…衆ロ難τιáo~ 提交于 2019-12-13 23:45:24

问题


Basically I am trying to simulate C pointer dereference using gnu-prolog.

Here is the code:

Prolog

:-foreign(fun(+integer,-integer)).

% p = &b;
testfun(Var, Val) :- fun(Val, Var).

main :-
A is 1,
testfun(A, P),
write(P),

C:

#include <gprolog.h>
#include <string.h>

PlBool fun(int ptr, int* res){
    *res = &ptr;                  // this is wrong
    if(res==NULL){
      return PL_FALSE;
    }else{
      return PL_TRUE;
    }
}

So basically it is wrong, because ptr is just a temp variable on the stack, and its memory will be deallocated after the calling fun.

So my question is, is it possible to get the variable's memory addresses in gnu prolog (For example, in this case, it is the address of A, not the address of ptr)?


回答1:


Gnu Prolog is pretty easy to extend by writing C routines and linking them into an executable. but if you are trying to "simulate the memory reference relation", then I'm doubtful that hacking in an actual memory address function would be useful.

Instead, as @lurker suggests, you likely want to "simulate" a memory/computer architecture and then some C-like language that "executes" on that. It sounds a little ambitious, but the simulation of a CPU in Prolog has been asked about already, and more recently here asked about by you, with Answer by lurker.



来源:https://stackoverflow.com/questions/23038033/is-it-possible-to-get-these-memory-addresses-in-gnu-prolog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!