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






This article was previously published under Q57923

SYMPTOMS

Microsoft QuickC version 2.0 or 2.5 routines that attempt memory allocation [with malloc()] do not allocate any memory when used in a Quick library in the QuickBasic environment. If the pointers are then referenced in the C routine, the following error message halts the program and returns to DOS:
run-time error R6013 - illegal far pointer use

STATUS

Microsoft has confirmed this to be a bug in Microsoft QuickC versions 2.0 and 2.5 (buglist2.00, buglist2.50); in the QB.EXE editor in Microsoft QuickBasic version 4.5; and in the QBX.EXE (QuickBasic Extended) editor in Microsoft Basic Professional Development System (PDS) versions 7.0 and 7.1 (buglist7.00, buglist7.10). We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

MORE INFORMATION

C routines that use malloc() to allocate memory work correctly when linked with compiled Basic programs. Furthermore, these routines work correctly in Quick libraries when compiled with C Compiler version 5.0 and 5.1.

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

Code Example

The example below 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. 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


/* 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 the above 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 as follows for QuickBasic 4.5:

LINK /Q /NOE setmem.c,,,bqlb45.lib;


Modification Type: Minor Last Reviewed: 1/9/2003
Keywords: KB57923