Microsoft KB Archive/116172

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

Article ID: 116172

Article Last Modified on 7/5/2005

-

APPLIES TO


 * Microsoft Visual C++ 1.0 Professional Edition
 * Microsoft Visual C++ 1.5 Professional Edition
 * Microsoft Visual C++ 1.0 Professional Edition
 * Microsoft Visual C++ 2.0 Professional Edition
 * Microsoft Visual C++ 4.0 Standard Edition
 * Microsoft Visual C++ 4.1 Subscription
 * Microsoft Visual C++ 4.2 Professional Edition

-



This article was previously published under Q116172



SYMPTOMS
When attempting to compile an application written in C++ that contains a based pointer initialized at global scope, the compiler incorrectly returns the 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.



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 an 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 that this is a bug in the Microsoft products that are listed at the beginning of this article.

Additional query words: kbVC400bug 8.00 8.00c 9.00

Keywords: kbbug kbcpponly kbcompiler KB116172

-

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

© Microsoft Corporation. All rights reserved.