Memory allocation: Difference between revisions
Content added Content deleted
(Added Wren) |
|||
Line 1,442: | Line 1,442: | ||
Unfortunately, you cannot type that in directly: not all 256 possible values are accessible from the keyboard, so there is no point trying to learn to enter machine code in that form. |
Unfortunately, you cannot type that in directly: not all 256 possible values are accessible from the keyboard, so there is no point trying to learn to enter machine code in that form. |
||
=={{header|Smalltalk}}== |
|||
Smalltalk does automatic memory management and garbage collection. |
|||
So in normal use, all you do is allocate by instantiating objects. |
|||
However, to support passing data in and out to external functions (typically: C-functions or data for a GPU or similar), |
|||
a number of additional APIs are present (which may differ slightly among Smalltalk dialects): |
|||
{{works with|Smalltalk/X}} |
|||
To allocate a non-movable, non garbage collected block of memory; (eg. to hand out a block of memory on which an external C-function keeps a reference). The memory must be explicitly freed by the programmer: |
|||
<lang smalltalk>handle := ExternalBytes new:size |
|||
... |
|||
handle free</lang> |
|||
To allocate a non-movable block of memory, which is garbage collected as soon as the reference is no longer reachable by Smalltalk (useful to hand out a block of memory to an external function which does NOT keep a reference on it: |
|||
<lang smalltalk>handle := ExternalBytes unprotectedNew:size |
|||
... |
|||
handle := nil "or no longer reachable" |
|||
... |
|||
block will be freed by the garbage collector eventually</lang> |
|||
Of course, both are to be used with great care, as memory leaks are possible. Thus, it is only used by core parts of the system, eg. for async I/O buffers, shared memory, mapped I/O devices etc. Normal programs would not use them. |
|||
=={{header|SNOBOL4}}== |
=={{header|SNOBOL4}}== |
||
In SNOBOL4, simple data values are just created and assigned to variables. Here, three separate strings are concatenated and stored as a newly allocated string variable: |
In SNOBOL4, simple data values are just created and assigned to variables. Here, three separate strings are concatenated and stored as a newly allocated string variable: |