Jump to content

Memory allocation: Difference between revisions

m
→‎{{header|Phix}}: added syntax colouring the hard way, phix/basics
m (→‎{{header|Phix}}: added syntax colouring the hard way, phix/basics)
Line 1,072:
 
=={{header|Phix}}==
{{libheader|Phix/basics}}
In normal use, memory management is fully automatic in Phix. However you may need to explicitly allocate memory when interfacing to C etc.
By default (for compatibility with legacy code) cleanup must be performed manually, but there is an optional flag on both the memory
allocation routines (allocate and allocate_string) to automate that for you, or you could even roll your own via delete_routine().
 
<lang Phix>atom addr = allocate(512) -- limit is 1,610,612,728 bytes on 32-bit systems
<!--<lang Phix>-->
...
<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;">512</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- limit is 1,610,612,728 bytes on 32-bit systems</span>
free(addr)
<span style="color: #0000FF;">...</span>
atom addr2 = allocate(512,1) -- automatically freed when addr2 drops out of scope or re-assigned
<span style="color: #7060A8;">free</span><span style="color: #0000FF;">(</span><span style="color: #000000;">addr</span><span style="color: #0000FF;">)</span>
atom addr3 = allocate_string("a string",1) -- automatically freed when addr3 drops out of scope or re-assigned</lang>
<span style="color: #004080;">atom</span> <span style="color: #000000;">addr2</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">allocate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">512</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- automatically freed when addr2 drops out of scope or re-assigned</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">addr3</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">allocate_string</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"a string"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- automatically freed when addr3 drops out of scope or re-assigned</span>
<!--</lang>-->
 
Behind the scenes, the Phix stack is actually managed as a linked list of virtual stack blocks allocated on the heap, and as such it
would be utterly pointless and quite probably extremely tricky to mess with.
7,820

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.