Pointers and references: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: added note about classes) |
m (→{{header|Phix}}: added syntax colouring the hard way) |
||
Line 1,218: | Line 1,218: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Phix does not have pointers, other than for playing with raw allocated memory, typically for interfacing with another language pre-compiled into a dll/so, although for that builtins/cffi.e offers a more grown-up mechanism with seamless 32/64 bit portability. There is no pointer math beyond sizes in bytes. |
Phix does not have pointers, other than for playing with raw allocated memory, typically for interfacing with another language pre-compiled into a dll/so, although for that builtins/cffi.e offers a more grown-up mechanism with seamless 32/64 bit portability. There is no pointer math beyond sizes in bytes. |
||
<lang Phix>atom addr = allocate(8) -- (assumes 32 bit) |
|||
⚫ | |||
poke4(addr,{NULL,SOME_CONSTANT}) |
|||
<span style="color: #004080;">atom</span> <span style="color: #000000;">addr</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">allocate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">8</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (assumes 32 bit)</span> |
|||
c_proc(xSome_External_Routine,{addr,addr+4}) |
|||
<span style="color: #7060A8;">poke4</span><span style="color: #0000FF;">(</span><span style="color: #000000;">addr</span><span style="color: #0000FF;">,{</span><span style="color: #004600;">NULL</span><span style="color: #0000FF;">,</span><span style="color: #000000;">SOME_CONSTANT</span><span style="color: #0000FF;">})</span> |
|||
?peek4s({addr,2}) -- prints {x,y} |
|||
<span style="color: #7060A8;">c_proc</span><span style="color: #0000FF;">(</span><span style="color: #000000;">xSome_External_Routine</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">addr</span><span style="color: #0000FF;">,</span><span style="color: #000000;">addr</span><span style="color: #0000FF;">+</span><span style="color: #000000;">4</span><span style="color: #0000FF;">})</span> |
|||
free(addr)</lang> |
|||
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">peek4s</span><span style="color: #0000FF;">({</span><span style="color: #000000;">addr</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">})</span> <span style="color: #000080;font-style:italic;">-- prints {x,y}</span> |
|||
<span style="color: #7060A8;">free</span><span style="color: #0000FF;">(</span><span style="color: #000000;">addr</span><span style="color: #0000FF;">)</span> |
|||
<!--</lang>--> |
|||
There are in fact 5 variants of poke: poke1, poke2, poke4, poke8, and pokeN which allows the size to be dynamically specified. |
There are in fact 5 variants of poke: poke1, poke2, poke4, poke8, and pokeN which allows the size to be dynamically specified. |
||
Likewise there are 9 versions of peek: peek1s, peek2s, peek4s, peek8s, peek1u, peek2u, peek4u, peek8u, and peekNS, and again the |
Likewise there are 9 versions of peek: peek1s, peek2s, peek4s, peek8s, peek1u, peek2u, peek4u, peek8u, and peekNS, and again the |
||
Line 1,254: | Line 1,258: | ||
UPDATE: as of 0.8.1, Phix has classes which ''are'' reference types, as are dictionaries.<br> |
UPDATE: as of 0.8.1, Phix has classes which ''are'' reference types, as are dictionaries.<br> |
||
Phix does not have references, however everything is passed by reference, with copy-on-write semantics. Unless the source and destination are the same, and it is local, so it cannot possibly be referenced elsewhere, in which case automatic pass-by-reference is used, which for Phix means skipping the reference counting. For example: |
Phix does not have references, however everything is passed by reference, with copy-on-write semantics. Unless the source and destination are the same, and it is local, so it cannot possibly be referenced elsewhere, in which case automatic pass-by-reference is used, which for Phix means skipping the reference counting. For example: |
||
⚫ | |||
<!--<lang Phix>--> |
|||
s = myfunc(s)</lang> |
|||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> |
|||
<span style="color: #0000FF;">...</span> |
|||
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">myfunc</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> |
|||
<!--</lang>--> |
|||
It is a general optimisation, applied by the complier whenever and wherever it can, without the programmer having to do anything special. |
It is a general optimisation, applied by the complier whenever and wherever it can, without the programmer having to do anything special. |
||
Several of the builtins, for instance s = append(s,thing) have a similar optimisation, as long as s occurs on both the rhs and lhs, and less any need for it to be local (since the builtins are non-recursive leaf routines). |
Several of the builtins, for instance s = append(s,thing) have a similar optimisation, as long as s occurs on both the rhs and lhs, and less any need for it to be local (since the builtins are non-recursive leaf routines). |