Microsoft KB Archive/57923

= QuickC 2.0 Routines Using malloc Fail in QB Quick Library =

Article ID: 57923

Article Last Modified on 11/21/2006



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 &quot;Memory not reallocated.&quot; 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;

Additional query words: QuickBas BasicCom 2.00 2.50 7.00 7.10 buglist4.50

Keywords: KB57923

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.