- This wiki is out of date, use the continuation of this wiki instead
Alloc
From FenixWiki
(Difference between revisions)
| Revision as of 14:27, 19 April 2007 (edit) Sandman (Talk | contribs) m (category) ← Previous diff |
Revision as of 23:00, 27 April 2007 (edit) (undo) Sandman (Talk | contribs) Next diff → |
||
| Line 13: | Line 13: | ||
| == Returns == | == Returns == | ||
| - | '''BYTE POINTER''' : | + | '''BYTE POINTER''' : Pointer to the first element of the allocated memory block. |
| + | |||
| + | == Throws == | ||
| + | {| | ||
| + | | Insufficient memory || - There is insufficient memory available. This error doesn't occur often. | ||
| + | |} | ||
| == Example == | == Example == | ||
| Line 20: | Line 25: | ||
| Private | Private | ||
| byte pointer pbyte; | byte pointer pbyte; | ||
| + | word pointer pword; | ||
| int pointer pint; | int pointer pint; | ||
| int elements = 10; | int elements = 10; | ||
| Line 27: | Line 33: | ||
| // Allocate memory | // Allocate memory | ||
| pbyte = alloc(elements); | pbyte = alloc(elements); | ||
| + | pword = alloc(elements*sizeof(word)); | ||
| pint = alloc(elements*sizeof(int)); | pint = alloc(elements*sizeof(int)); | ||
| // Reset memory to 0's | // Reset memory to 0's | ||
| - | memset(pbyte,0,elements); | + | memset (pbyte,0,elements); |
| - | memset(pint ,0,elements*sizeof(int)); | + | memsetw(pword,0,elements); // same as memset(pword,0,elements*sizeof(word)); |
| + | // because value-parameter is 0. | ||
| + | memset (pint ,0,elements*sizeof(int)); // There isn't a "memseti()", so we need to | ||
| + | // set the individual bytes to 0. To change | ||
| + | // ints to nonzero values, memset() can't be | ||
| + | // used easily | ||
| - | // Write numbers to | + | // Write numbers to bytes and ints |
| for(i=0; i<elements; i++) | for(i=0; i<elements; i++) | ||
| - | pbyte[i] = 133; | + | pbyte[i] = 133; // pbyte[i] is the same as *(pbyte+i) |
| - | *(pint+i) = 4555; | + | *(pint+i) = 4555; // pint[i] is the same as *(pint+i) |
| end | end | ||
| + | |||
| + | // Write numbers to words | ||
| + | memsetw(pword,345,elements); | ||
| // Show numbers | // Show numbers | ||
| for(i=0; i<elements; i++) | for(i=0; i<elements; i++) | ||
| say("byte["+i+"] = " + *(pbyte+i)); | say("byte["+i+"] = " + *(pbyte+i)); | ||
| + | say("word["+i+"] = " + pword[i]); | ||
| say("int ["+i+"] = " + pint[i]); | say("int ["+i+"] = " + pint[i]); | ||
| end | end | ||
| Line 48: | Line 64: | ||
| frame; | frame; | ||
| End | End | ||
| + | |||
| End | End | ||
| </pre> | </pre> | ||
| - | Used in example: [[memset]](), [[ | + | Used in example: [[memset]](), [[memsetw]](), [[sizeof]](), [[say]](), [[pointer]] |
Revision as of 23:00, 27 April 2007
Contents |
Definition
BYTE POINTER Alloc ( <INT size> )
Allocates a block of memory of a certain size.
Parameters
| INT size | - The size of the to be allocated memory. |
Returns
BYTE POINTER : Pointer to the first element of the allocated memory block.
Throws
| Insufficient memory | - There is insufficient memory available. This error doesn't occur often. |
Example
Program example;
Private
byte pointer pbyte;
word pointer pword;
int pointer pint;
int elements = 10;
int i;
Begin
// Allocate memory
pbyte = alloc(elements);
pword = alloc(elements*sizeof(word));
pint = alloc(elements*sizeof(int));
// Reset memory to 0's
memset (pbyte,0,elements);
memsetw(pword,0,elements); // same as memset(pword,0,elements*sizeof(word));
// because value-parameter is 0.
memset (pint ,0,elements*sizeof(int)); // There isn't a "memseti()", so we need to
// set the individual bytes to 0. To change
// ints to nonzero values, memset() can't be
// used easily
// Write numbers to bytes and ints
for(i=0; i<elements; i++)
pbyte[i] = 133; // pbyte[i] is the same as *(pbyte+i)
*(pint+i) = 4555; // pint[i] is the same as *(pint+i)
end
// Write numbers to words
memsetw(pword,345,elements);
// Show numbers
for(i=0; i<elements; i++)
say("byte["+i+"] = " + *(pbyte+i));
say("word["+i+"] = " + pword[i]);
say("int ["+i+"] = " + pint[i]);
end
Loop
frame;
End
End
Used in example: memset(), memsetw(), sizeof(), say(), pointer
