Pointer Directives

Pointer directives start with ampersand (:&).

U has pointer management for low level coding and legacy code bases imports (C/C++). When using pointer directives, you have full access to hardware, but this also means your code is not guaranteed to be portable and safe.

U assists you in first converting an existing code base to U with unsafe parts, then remove unsafe features with the help of U compiler. It's a progressive process towards a reliable application.

Main pointer conversions are:

  • From typed memory locations to values, use :&< (address at):

        # Map Raspberry GPIO led at memory location 0x3e000000
        # ':!' bind mutable variable
        :led :! :&<int 0x3e000000
    

    The arrow in &< reminds you that U converts the right-hand side U number value to a typed memory location in the outside host value. See Source Code Model.

  • From values to typed memory locations, use :&> (content at):

        # Switch on LED
        :&> led := 1
        # Or
        led :&= 1
    

    The arrow in &> reminds you that U converts a typed memory location from the outside host value to a U value. See Source Code Model.

U raises an error if you use &=, as led is a memory location, not a number.

You can use keywords too:

::pa 0x3e000000 # <=> :&<, Pointer Address
::pv led        # <=> :&>, Pointer Value