ex.hlp (Topic list)
Important Notice
The pages on this site contain documentation for very old MS-DOS software, purely for historical purposes. If you're looking for up-to-date documentation, particularly for programming, you should not rely on the information found here, as it will be woefully out of date.
SETMEM Function 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.
 
' To try this example:
' 1. Choose New Project from the File menu
' 2. Copy the code example below to the code window
' 3. Press F5 to run the example
 
 DECLARE SUB CFunc CDECL (BYVAL X AS INTEGER)
 
 BeforeCall = SETMEM(-2048) ' Decrease the size of the far heap so
                            ' CFunc can use malloc to get dynamic memory
 
 CFunc (1024)               ' Call the C function
 
 AfterCall = SETMEM(3500)   ' Return the memory to the far heap; use a larger
                            ' value so all space goes back into the heap
 
 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);
' }