BUG: Based Ptr. Init Fails at Global Scope in CPP File

Last reviewed: July 31, 1997
Article ID: Q116172
The information in this article applies to:
  • The Microsoft C/C++ Compiler (CL.EXE) included with: - Microsoft C/C++ for MS-DOS, version 7.0 - Microsoft Visual C++ for Windows, versions 1.0, 1.5 - Microsoft Visual C++ 32-bit Edition, versions 1.0, 2.0, 4.0, 4.1, 4.2,

         5.0
    

SYMPTOMS

When attempting to compile an application written in C++ that contains a based pointer initialized at global scope, the compiler incorrectly returns the following error message:

   error C2440 : 'initializing' : cannot convert from 'int __far *' to
    'int __based(xxxx) *'

where xxxx is the segment on which the pointer is based.

This error does not occur for based pointers that are initialized at function scope.

RESOLUTION

Sample Code 1

With the 16-bit compilers listed above, this error can be eliminated by typecasting the constant that is used to initialize the based pointer to a type "_based(void) *". The following code demonstrates how to generate the compiler error as well as the workaround:

   /* Compile options needed:   none
   */

   // Base Pointer
   int *BasePtr;

   // This line compiles fine (no initialization)
   int _based(BasePtr) *test1;

   // This line generates C2440 error
   int _based(BasePtr) *test2=0;

   // This line demonstrates the 16 bit workaround
   int _based(BasePtr) *test3 = (int _based(void) *)0;

   void main(void)
   {
   // Initialization at file scope does not generate C2440

      int _based(BasePtr) *test4 = 0;
   }

Sample Code 2

In the 32-bit compiler for Windows NT, version 8.0, the workaround given for the 16-bit compilers does not work. The "_based(void) *" cast generates the following two error messages if you are using the 32-bit compiler:

   error C2493: illegal form of __based
   error C2440: 'initializing' : cannot convert from 'int *' to 'int
   __based(BasePtr) *'

In this case, the file scope based pointer cannot be declared and initialized in one step. An appropriate workaround would be to declare the pointer at file scope and initialize the pointer inside of a function. The following code demonstrates how to generate the compiler error as well as the workaround:

   /* Compile options needed: none
   */
   // Base Pointer
   int *BasePtr;

   // This line compiles fine (no initialization).
   int _based(BasePtr) *test1;

   // This line generates C2440 error.
   int _based(BasePtr) *test2=0;

   // This line generates C2440 and C2493 errors.
   int _based(BasePtr) *test3 = (int _based(void) *)0;

   void main(void)
   {
   // 32-bit workaround: initialize the based pointer at function scope.

      test1 = 0;

   // Initialization at file scope does not generate C2440 or C2493.

      int _based(BasePtr) *test4 = 0;
   }

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.


Additional query words: 8.00 8.00c 9.00
Keywords : CPPIss vcbuglist400 vcbuglist500
Version : 7.0 1.0 1.5 2.0 4.0 4.1 4.2 5.0
Platform : MS-DOS NT WINDOWS
Issue type : kbbug


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 31, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.