QuickC 2.0 Routines Using malloc() Fail in QB Quick Library

This article was previously published under Q57923
This article has been archived. It is offered "as is" and will no longer be updated.
Microsoft QuickC version 2.0 or 2.5 routines that attempt memoryallocation [with malloc()] do not allocate any memory when used in aQuick library in the QuickBasic environment. If the pointers are thenreferenced in the C routine, the following error message halts theprogram and returns to DOS:
run-time error R6013 - illegal far pointer use
Microsoft has confirmed this to be a bug in Microsoft QuickCversions 2.0 and 2.5 (buglist2.00, buglist2.50); in the QB.EXE editorin Microsoft QuickBasic version 4.5; and in the QBX.EXE (QuickBasicExtended) editor in Microsoft Basic Professional Development System(PDS) versions 7.0 and 7.1 (buglist7.00, buglist7.10). We areresearching this problem and will post new information here in theMicrosoft Knowledge Base as it becomes available.
C routines that use malloc() to allocate memory work correctly whenlinked with compiled Basic programs. Furthermore, these routines workcorrectly in Quick libraries when compiled with C Compiler version 5.0and 5.1.

The following code example causes the above error (R6013) in theQuickBasic environment. The code is the SETMEM example from theQuickBasic QB Advisor online Help system, with one line added toreference the pointer.

Code Example

The example below uses the SETMEM function to free memory for a Cfunction that uses the C routine malloc() to get dynamic memory.

Note: To run this program, you must separately compile the C functionand put it in a Quick library. The C function must be compiled usingthe large memory model, so calls to malloc() use the far space freedby 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/* Filename: Setmemc.c,  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. */    *workspace='X';    // Added line to reference ptr/cause error   /* Free memory before returning to Basic. */    free(workspace);}				
The following are the QuickC and C compiler compiling lines for theabove C code:
QCL -c -AL setmemc.c ;
CL -c -AL setmemc.c ;
To create a Quick library from the C routine, the link line is asfollows for QuickBasic 4.5:
LINK /Q /NOE setmem.c,,,bqlb45.lib;
QuickBas BasicCom 2.00 2.50 7.00 7.10 buglist4.50

Article ID: 57923 - Last Review: 02/27/2014 21:00:11 - Revision: 1.2

  • kbnosurvey kbarchive KB57923