Microsoft KB Archive/122540: Difference between revisions

From BetaArchive Wiki
m (Text replacement - ">" to ">")
m (Text replacement - "&" to "&")
 
Line 12: Line 12:
<div id="TitleRow">
<div id="TitleRow">


= <span id="KB122540"></span>FIX: 'char *' Passed to 'const char * &amp;' Violates Type Safety =
= <span id="KB122540"></span>FIX: 'char *' Passed to 'const char * &' Violates Type Safety =




Line 61: Line 61:
== RESOLUTION ==
== RESOLUTION ==


To protect the const data, the reference should be a reference to a const pointer to a const, that is: const char * const &amp;.
To protect the const data, the reference should be a reference to a const pointer to a const, that is: const char * const &.


</div>
</div>
Line 89: Line 89:
onst char * const_ptr = "This is a 'const char *'";
onst char * const_ptr = "This is a 'const char *'";


void func ( const char* &amp; reference) {
void func ( const char* & reference) {
   reference = const_ptr;
   reference = const_ptr;
}
}

Latest revision as of 12:26, 21 July 2020

Knowledge Base


Article ID: 122540

Article Last Modified on 7/5/2005



APPLIES TO

  • Microsoft Visual C++ 1.0 Professional Edition
  • Microsoft Visual C++ 2.0 Professional Edition
  • Microsoft Visual C++ 2.1
  • Microsoft Visual C++ 4.0 Standard Edition
  • Microsoft Visual C++ 4.1 Subscription
  • Microsoft Visual C++ 4.2 Enterprise Edition
  • Microsoft Visual C++ 5.0 Enterprise Edition
  • Microsoft Visual C++ 4.2 Professional Edition
  • Microsoft Visual C++ 5.0 Professional Edition
  • Microsoft Visual C++ 1.5 Professional Edition
  • Microsoft Visual C++ 1.52 Professional Edition



This article was previously published under Q122540

SYMPTOMS

Visual C++ incorrectly allows a pointer to be passed to a function that takes a non-const reference to a pointer to a const. This breaks C++ type safety and allows the const data assigned to the reference to be modified by the pointer after the function has returned.

RESOLUTION

To protect the const data, the reference should be a reference to a const pointer to a const, that is: const char * const &.

STATUS

This bug was corrected in Microsoft Visual C++, version 6.0.

MORE INFORMATION

A reference may only be initialized with an lvalue expression of the same type or a class derived from that type. If this is not the case, the reference must be declared const.

The following sample includes a non-const reference of type const char*, which is being initialized with an expression of type char*. The compiler should, but does not, generate an error. The program prints this string:

const_ptr = Xhis is a 'const char *'
                

Sample Code

/* Compile options needed: none
*/ 

#include <iostream.h>

onst char * const_ptr = "This is a 'const char *'";

void func ( const char* & reference) {
   reference = const_ptr;
}

void main() {
//  char *non_const_ptr = const_ptr;  // Not allowed.
   char *non_const_ptr;

//  *const_ptr = 'X';      // Not allowed.

   func( non_const_ptr );
   // 
   //  'const char *' is violated now.
   // 
   *non_const_ptr = 'X';    // Mess up first letter of string

   cout << "const_ptr = " << const_ptr << endl;

}
                


Additional query words: 8.0c 8.00c 9.0 9.00 10.00 10.10 10.20

Keywords: kbbug kbfix kbvc600fix KB122540