cAlloc Function

Purpose

Allocates an array in memory with elements initialized to 0.

Syntax

long = cAlloc( num, size )

num, size:iexp

Description

cAlloc() returns a pointer to the allocated space. num specifies the number of elements and size specifies the length in bytes of each element. The reserved memory block is initialized with 0.

cAlloc() is implemented to easily port C-source code. Compare the internal implementation in both C and GFA-BASIC 32:

The allocated memory can be resized using mReAlloc or mShrink and released with MFree.

The C- implementation

void *calloc(int a, int b)
{
    void *p = malloc(a * b);
    if(p) memset(p, 0, a * b);
    return p;
}

The GFA-BASIC 32 implementation

Function cAlloc(a As Int, b As Int) As Int

Local p As Int = mAlloc(a * b)

If(p) Then MemSet(p, 0, a * b)

Return p

End Func

Example

Dim p As Long = cAlloc(10, SizeOf(Int))

Allocates 40 bytes (10 * 4), because the size of an Int data type is 4 bytes.

Remarks

C GFA-BASIC 32
malloc mAlloc
calloc cAlloc
realloc mReAlloc or mShrink
free mFree
memset(a, v, n) MemSet(a, v, n) or MemBFill a, n, v
memcpy(d, s, n) MemCpy(d, s, n)

See Also

mAlloc(), mFree(), mShrink(), mReAlloc()

{Created by Sjouke Hamstra; Last updated: 03/03/2017 by James Gaite}