Memory allocation: Difference between revisions

Content added Content deleted
(→‎{{header|Pascal}}: add example)
Line 449: Line 449:
But if desired, memory can be pre-allocated by calling [http://software-lab.de/doc/refG.html#gc gc] with a single numeric argument, specifying the desired number of megabytes that should be reserved. When that argument is zero, the heap size is decreased (as far as possible).
But if desired, memory can be pre-allocated by calling [http://software-lab.de/doc/refG.html#gc gc] with a single numeric argument, specifying the desired number of megabytes that should be reserved. When that argument is zero, the heap size is decreased (as far as possible).


=={{header|PL/I}}==
=={{header|Mathematica}}==
Mathematica allocates memory and garbage collects it.
PL/I can allocate memory in three ways


If desired, memory can be optimized/reclaimed somewhat by calling Share[] or ClearSystemCache[].
* On the stack: this happens with variables declared as automatic in procedures and begin blocks. The variables are automatically freed when at exit of the procedure or block. If an ''init'' clause is specified, the variable will also be (re-)initialized upon entry to the procedure or block. If no ''init'' clause is specified, the variable will most likely contain garbage. An example:

<lang pli>
mainproc: proc options(main) reorder;

subproc: proc;
dcl subvar char init ('X');

put skip data(subvar);
subvar = 'Q';
end subproc;

call subproc();
call subproc();
end mainproc;</lang>

Result:
<pre>subvar='X';
subvar='X';</pre>

* On the heap: if a variable is declared with the ''ctl'' (or in full, ''controlled'') attribute, it will be allocated from the heap and multiple generations of the same variable can exist, although only the last one allocated can be accessed directly. An example:

<lang pli>
mainproc: proc options(main) reorder;
dcl ctlvar char ctl;

alloc ctlvar;
ctlvar = 'A';
alloc ctlvar;
ctlvar = 'B';
alloc ctlvar;
ctlvar = 'C';

put skip data(ctlvar);
free ctlvar;
put skip data(ctlvar);
free ctlvar;
put skip data(ctlvar);
free ctlvar;
end mainproc;</lang>

Result:
<pre>ctlvar='C';
ctlvar='B';
ctlvar='A';</pre>

* On the heap: if a variable is declared with the ''based'' attribute, it will be allocated from the heap and multiple generations of the same variable can exist. This type of variable is often used in linked lists. An example:

<lang pli>
mainproc: proc options(main) reorder;
dcl list_ptr ptr init (sysnull());
dcl list_top ptr init (sysnull());
dcl list_end ptr init (addr(list_top));
dcl i fixed bin (31);
dcl 1 list based(list_ptr),
2 list_nxt ptr init (sysnull()),
2 list_data fixed bin (31) init (i);

/*
* Allocate the list
*/
do i = 1 to 4;
alloc list;
list_end -> list_nxt = list_ptr;
list_end = list_ptr;
end;

/*
* Print the list
*/
do list_ptr = list_top repeat list_nxt
while(list_ptr ^= sysnull());
put skip list(list_data);
end;
end mainprog;</lang>
Result:
<pre>1
2
3
4</pre>


=={{header|PureBasic}}==
=={{header|PureBasic}}==