- This wiki is out of date, use the continuation of this wiki instead
Realloc
From FenixWiki
(Difference between revisions)
Revision as of 00:28, 28 April 2007 (edit) Sandman (Talk | contribs) ← Previous diff |
Revision as of 15:24, 28 April 2007 (edit) (undo) Sandman (Talk | contribs) m Next diff → |
||
Line 7: | Line 7: | ||
Resizes the given block of memory. | Resizes the given block of memory. | ||
- | It allocates a new block of memory, copying the old data. If the new size is smaller than the old size, the last part of the data is lost. | + | It allocates a new block of memory, copying the old data. If the new size is smaller than the old size, the last part of the data is lost. If the new size of the block of memory requires movement of the block, the old memory block is freed. |
== Parameters == | == Parameters == | ||
Line 13: | Line 13: | ||
| '''VOID POINTER''' data || - Pointer to the block of memory to be resized. | | '''VOID POINTER''' data || - Pointer to the block of memory to be resized. | ||
|- | |- | ||
- | | '''INT''' size || - The size of the | + | | '''INT''' size || - The new size of the block of memory in bytes. |
|} | |} | ||
Line 44: | Line 44: | ||
pbyte2 = realloc(pbyte,newelements); | pbyte2 = realloc(pbyte,newelements); | ||
- | + | // Set the added part's elements to 16 (newelements > elements) | |
- | + | memset(pbyte+elements,16,newelements-elements); | |
// Show numbers | // Show numbers | ||
Line 52: | Line 52: | ||
end | end | ||
- | | + | Repeat |
frame; | frame; | ||
- | | + | Until(key(_esc)) |
+ | |||
+ | // Free the used memory | ||
+ | free(pbyte2); | ||
End | End | ||
</pre> | </pre> | ||
- | Used in example: [[alloc]](), [[memset]](), [[say]](), [[pointer]] | + | Used in example: [[alloc]](), [[memset]](), [[say]](), [[free]](), [[pointer]] |
Revision as of 15:24, 28 April 2007
Contents |
Definition
VOID POINTER realloc ( <VOID POINTER data> , <INT size> )
Resizes the given block of memory.
It allocates a new block of memory, copying the old data. If the new size is smaller than the old size, the last part of the data is lost. If the new size of the block of memory requires movement of the block, the old memory block is freed.
Parameters
VOID POINTER data | - Pointer to the block of memory to be resized. |
INT size | - The new size of the block of memory in bytes. |
Returns
VOID POINTER : Pointer to (the first element of) the newly allocated memory block.
Errors
Insufficient memory | - There is insufficient memory available. This error doesn't occur often. |
Example
Program example; Private byte pointer pbyte; byte pointer pbyte2; int elements = 10; int newelements = 15; int i; Begin // Allocate memory pbyte = alloc(elements); // Set them to 13 memset(pbyte,13,elements); // Relocate it to a larger, newly made memory block pbyte2 = realloc(pbyte,newelements); // Set the added part's elements to 16 (newelements > elements) memset(pbyte+elements,16,newelements-elements); // Show numbers for(i=0; i<newelements; i++) say("byte2["+i+"] = " + pbyte2[i]); end Repeat frame; Until(key(_esc)) // Free the used memory free(pbyte2); End