Tutorial :Changing the value of a const pointer



Question:

I have the following piece of code:

  void TestFunc(const void * const Var1, const float Var2)  {    *(float*)Var1 = Var2;  }  

It looks like I am changing the value of the const object the const pointer points to (thanks sharptooth), which should not be allowed. Fact is, none of the compilers I tried issued a warning. How is this possible?


Solution:1

As others mentioned, the cast removes the 'constness' of the destination as far as the expression is concerned. When you use a cast the compiler treats the expression according to the cast - as long as the cast itself it valid (and C-style casts are pretty much the big hammer). This is why you don't get an error or warning. You're essentially telling the compiler, "be quiet, I know what I'm doing, this is how you should treat things". In fact, casts are probably the #1 way for programmers to get the compiler to stop issuing warnings.

Your assignment expression may or may not be undefined behavior. It is permitted to cast away constness if the object actually pointed to is not const.

However, if the object pointed to is const, then you have undefined behavior.

void TestFunc(const void * const Var1, const float Var2)  {    *(float*)Var1 = Var2;  }      int  main(void)  {      float x = 1.0;      const float y = 2.0;        TestFunc( &x, -1.0);    // well defined (if not particularly great style)      TestFunc( &y, -2.0);    // undefined behavior        return 0;  }  

You're treading dangerous waters...

In general (I'm sure there are exceptions), casting so that expressions treat objects as they really are is supported, well-defined behavior in C/C++.

This particular behavior is covered in the standards mostly by statements that modifying a const object through a cast (or something) that removes the const qualifier is undefined. The inference is that doing the same for a non-const object is not undefined. An example given in the C++ standard makes this clear.

C90 6.5.3 - Type Qualifiers (C99 6.7.3):

If an attempt is made to modify an object defined with a const-qualified type through use of an lvalue with non-const-qualified type, the behavior is undefined.

C++ 7.1.5.1 The cv-qualifiers

A pointer or reference to a cv-qualified type need not actually point or refer to a cv-qualified object, but it is treated as if it does; a const-qualified access path cannot be used to modify an object even if the object referenced is a non-const object and can be modified through some other access path. [Note: cv-qualifiers are supported by the type system so that they cannot be subverted without casting (5.2.11). ]

Except that any class member declared mutable (7.1.1) can be modified, any attempt to modify a const object during its lifetime (3.8) results in undefined behavior.

...

[Example:

...

int i = 2;            //not cv-qualified  const int* cip;       //pointer to const int  cip = &i;             //OK: cv-qualified access path to unqualified  *cip = 4;             //ill-formed: attempt to modify through ptr to const    int* ip;  ip = const_cast<int*>(cip);   //cast needed to convert const int*to int*  *ip = 4;                      //defined: *ip points to i, a non-const object    const int* ciq = new const int (3);   //initialized as required  int* iq = const_cast<int*>(ciq);      //cast required  *iq = 4;                              //undefined: modifies a const object  


Solution:2

You change the object the pointer points to, not the pointer value.

C-style cast acts like a const_cast and removes the const modifier off the pointer. The compiler now has nothing to moan about.


Solution:3

The cast is legal, but the behaviour is undefined.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »