是否可以在gnu序言中获得这些内存地址? [英] Is it possible to get these memory addresses in gnu prolog?

查看:89
本文介绍了是否可以在gnu序言中获得这些内存地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我正在尝试使用gnu-prolog模拟C指针取消引用.

Basically I am trying to simulate C pointer dereference using gnu-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;
    }
}

所以基本上这是错误的,因为ptr只是堆栈上的一个临时变量,在调用fun之后它的内存将被释放.

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.

所以我的问题是,是否可以在gnu序言中获取变量的内存地址(例如,在这种情况下,它是A的地址,而不是ptr的地址)?

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)?

推荐答案

通过

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.

相反,正如@lurker所建议的那样,您可能想要模拟"内存/计算机体系结构,然后模拟"类似的C语言.听起来有些雄心勃勃,但是在Prolog中模拟CPU a>已经被询问过,最近在这里询问您的问题,以及潜行者的答案".

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.

这篇关于是否可以在gnu序言中获得这些内存地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆