assign/2

Synopsis

assign(+LHS, +Expr)

Evaluates Expr as an arithmetic expression, and stores the value in the memory location and format given by LHS.

Arguments


LHS compound
One of the following terms
L_Exp expr
a valid arithmetic expression
Expr expr
A valid arithmetic expression

Description

Can be used to poke data directly into memory. Evaluates L_Exp in LHS and Expr as arithmetic expressions. The functor of the first argument describes the type of data to be stored: integer_8_at/1 will store a signed 8 bit integer, single_at/1 will store a single precision floating point number, etc. For more structured ways of doing this, see the Structs and Objects packages.

The only proper addresses that should be assigned to are ones that are obtained through the foreign interface. assign/2 is a very primitive built-in and users should have only very rare occasions to use it. To directly access and change data structures represented in foreign languages (like C) users should look at using the Structs and Objects packages.

Both arguments can be unbound at compile time. But it is more efficient if LHS is bound at compile time. Also note that attempting to overwrite improper locations of memory can cause "Segmentation faults" or "Bus errors" and overwriting Prolog memory can result in undesirable behaviour long after the assignment is done.

Exceptions


instantiation_error
LHS or Expr is not ground
type_error
Expr is not a proper arithmetic expression or

L_Exp in LHS is not a proper arithmetic expressions or

Expr is of a different type than what is specified by LHS

domain_error
LHS is not one of the above listed terms

Examples

                                      
foo.c
static int counter; int * init_counter() { counter = 0; return &counter; }
                                     
foo.pl
foreign(init_counter, c, init_counter([-address])). get_counter(Counter, Count) :- Count is integer_at(Counter). incr_counter(Counter) :- assign(integer_at(Counter), integer_at(Counter)+1). | ?- init_counter(C), incr_counter(C), get_counter(C, Count1), incr_counter(C), incr_counter(C), get_counter(C, Count2). C = 1418304, Count1 = 1, Count2 = 3 | ?-

See Also

ref-ari-aex library(structs), library(objects)