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

Realloc

From FenixWiki

Revision as of 15:24, 28 April 2007 by Sandman (Talk | contribs)
Jump to: navigation, search


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

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

Personal tools