This wiki is out of date, use the continuation of this wiki instead

Realloc

From FenixWiki

(Difference between revisions)
Jump to: navigation, search
Revision as of 00:28, 28 April 2007 (edit)
Sandman (Talk | contribs)

← Previous diff
Current revision (12:49, 29 April 2008) (edit) (undo)
Sandman (Talk | contribs)
m
 
(One intermediate revision not shown.)
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 to be allocated memory.+| '''INT''' size || - The new size of the block of memory in bytes.
|} |}
Line 52: Line 52:
end end
- Loop+ Repeat
frame; frame;
- End+ 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]](), [[realloc]](), [[say]](), [[free]](), [[pointer]]
 + 
 +{{Funcbox
 + | category = Memory
 +}}

Current revision


Contents

[edit] 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.

[edit] 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.

[edit] Returns

VOID POINTER : Pointer to (the first element of) the newly allocated memory block.

[edit] Errors

Insufficient memory - There is insufficient memory available. This error doesn't occur often.

[edit] 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

Used in example: alloc(), memset(), realloc(), say(), free(), pointer


Memory Functions
Alloc() • Free() • Memcmp() • Memcopy() • Memory_free() • Memory_total() • Memset() • Memsetw() • Realloc() •
Personal tools