alloc, free - Allocate blocks of memory
alloc(
n )
free( p )
alloc() allocates a block of memory with n locations and returns the address of the first location.
A block of memory refers to a series of neighboring memory locations and the return value of this function is the first memory address in the block.
free() (Currently unimplemented) returns previously allocated memory blocks to the free list. The argument p should be the memory address returned by an earlier call to alloc().
alloc() returns the address of the first location of the block of memory allocated.
free() has no return value.
free() has not yet been implemented.
If you are struggling with understanding this function, consider thinking of var as creating an individual variable and alloc() as creating a group of variables. This group will consist of several locations next to each other in memory. This is called a block of memory. You can assign a value to every member of this group by adding an "index" onto the first address.
var block block : alloc(16) (.block) : ’a’ # sets the value of the first location of the block (.block + 6) : ’f’ # sets the value of the sixth location of the block
A null byte (0) can be used to mark the termination of the block. This "null terminator" is necessary for strings.
The following
example from page 7 of the Tranquility Programmer’s
Manual
https://www.cs.drexel.edu/~bls96/tranquility.pdf
creates a table of factorials.
fun fact(n) { if .n == 0 { return 1 } else { return .n * fact(.n - 1) } } fun genfacts(n) { var i, ftab ftab : alloc(.n) i : 0 loop { until .i >= .n (.ftab + .i) : fact(.i) i : .i + 1 } return .ftab } fun filltable() { var i, ftab, istr html("<center>\n") html("<table border=1><tr><th>n</th><th>n!</th></tr>\n") ftab : genfacts(13) istr : alloc(12) i : 0 loop { until .i > 12 html("<tr><td>") i2s(.istr, .i) html(.istr) html("</td><td>") i2s(.istr, .(.ftab + .i)) html(.istr) html("</td></tr>\n") i : .i + 1 } html("</table></center>\n") } fun init () { html("<center>") button("Make Table", filltable) html("<p>Factorials</p>\n</center>\n") }
Charlie Stuart
cstuart11@protonmail.com
Michael Hadad, Fall 21-22
First, check
the Tranquility Programmer’s Manual
https://www.cs.drexel.edu/~bls96/tranquility.pdf
If the problem persists see Dr. Stuart or Charlie Stuart
Dr. Stuart:
brian.l.stuart@drexel.edu
Charlie Stuart:
cstuart11@protonmail.com
tranqc(1), i2s(3), sread(3)
Tranquility
Programmer’s Manual
https://www.cs.drexel.edu/~bls96/tranquility.pdf