Click or drag to resize

MemAlloc Function

Allocate a static memory buffer of a specified size.

Namespace:  XSharp.RT
Assembly:  XSharp.RT (in XSharp.RT.dll) Version: 2.08
Syntax
 FUNCTION MemAlloc(
	wBytes AS DWORD
) AS IntPtr
View Source

Parameters

wBytes
Type: UInt32
The number of bytes to allocate.

Return Value

Type: IntPtr
A pointer to the allocated space if there is sufficient memory available; otherwise, it returns a NULL_PTR.  You should always check the return value for a successful allocation.
Remarks
MemAlloc() allocates a memory buffer of at least <wBytes>.  The allocated memory is defined to be static so it is not moved by the garbage collector.  Since memory is allocated in increments of 32 bytes, some extra space may be allocated.  For example, a request for even 1 byte will allocate 32 bytes.  Space allocated by MemAlloc() is freed by MemFree().
Tip Tip
This function allows the direct manipulation of a memory location and should be used with extreme care.
Remarks
Tip Tip
The Static Memory Functions (MemAlloc, MemSet etc) are included for compatibility only. In most cases the static memory blocks can (and should) be replaced with arrays of bytes.
Many of the functions in the runtime that take memory blocks as parameter, such as the low level IO functions, now have overloads that take arrays of bytes as parameter.
We recommend that you use these overloads, because their performance is somewhat better.
Examples
This example uses MemAlloc() to see whether a specified block is available:
X#
FUNCTION IsAvailable(wRequest AS DWORD) AS LOGIC
        LOCAL ptrBuff AS PTR
        LOCAL lAvailable AS LOGIC
        ptrBuff := MemAlloc(wRequest)
        IF ptrBuff = NULL_PTR
                ? "Memory allocation has failed"
                lAvailable := FALSE
        ELSE
                lAvailable := TRUE
                MemFree(ptrBuff)
        ENDIF
        RETURN lAvailable
See Also