SETMEM Function Programming Example
◄Example► ◄Contents► ◄Index► ◄Back►
──────────────────────────────────────────────────────────────────────────────
'This example uses the SETMEM function to free memory for a C function that
'uses the C routine malloc to get dynamic memory.
'Note: To run this program, you must separately compile the C function and
'put it in a Quick library or link it to the BASIC program. The C function
'must be compiled using the large memory model, so calls to malloc use the
'far space freed by the BASIC program.
DECLARE SUB CFunc CDECL (BYVAL X AS INTEGER)
'Decrease the size of the far heap so CFunc can use malloc
'to get dynamic memory.
BeforeCall = SETMEM(-2048)
'Call the C function.
CFunc(1024%)
'Return the memory to the far heap; use a larger value so
'all space goes back into the heap.
AfterCall = SETMEM(3500)
IF AfterCall <= BeforeCall THEN PRINT "Memory not reallocated."
END
'/* C Function */
'void far cfunc(bytes)
'int bytes;
'{
' char *malloc();
' char *workspace;
'
' /* Allocate working memory using amount BASIC freed. */
' workspace=malloc((unsigned) bytes);
'
' /* Working space would be used here. */
'
' /* Free memory before returning to BASIC. */
' free(workspace);
'}