Type Casting Const Pointers to non const pointers 
Author Message
 Type Casting Const Pointers to non const pointers

Hi,

It appears the the Microsoft compiler is allowing me to type cast a const
pointer to a non const pointer (See example below).  Is this normal?  Does
anyone know of a compilter switch that I can turn on to prevent other
programers from updating my const data?

Thanks
Das

#include <stdio.h>

void Test(const char* const pData)
{
    char *pTemp = (char*)pData;
    *pTemp = 'B';

Quote:
}

void main()
{
    const char *pContString = "Hello";

    Test(pContString);
    printf(pContString);

Quote:
}



Sat, 26 Mar 2005 19:19:15 GMT  
 Type Casting Const Pointers to non const pointers

Quote:

> Hi,

> It appears the the Microsoft compiler is allowing me to type cast a const
> pointer to a non const pointer (See example below).  Is this normal?  Does
> anyone know of a compilter switch that I can turn on to prevent other
> programers from updating my const data?

> Thanks
> Das

I would be very surprised if any C/C++ compiler was different.  That's
what casts do: Their major purpose is to bypass compiler rules.  In the
C/C++ language you have absolutely no control of other programmers.
That's probably why so many of us use the language :)

--
Scott McPhillips [VC++ MVP]



Sat, 26 Mar 2005 20:04:59 GMT  
 Type Casting Const Pointers to non const pointers


Quote:
> Hi,
Hi.

> It appears the the Microsoft compiler is allowing me to type cast a const
> pointer to a non const pointer (See example below).  Is this normal?  Does
> anyone know of a compilter switch that I can turn on to prevent other
> programers from updating my const data?

Yep, it's normal and necessary for compatibility with some old code. Is is
indeed a very bad idea, but as Stroustrup said : "C++ make it harder to
shoot oneself in the foot, but when you do it, it blows all your leg away"
:-)

What would be good is to have at least a warning. Have you tried compiling
with Level 4 warning?

Arnaud
MVP - VC



Sat, 26 Mar 2005 20:18:36 GMT  
 Type Casting Const Pointers to non const pointers

Hi,

Compiling the following code in Level 4, gives no warning at all (VC 6 -
Win2000).
The result I get is "10 - 20 - 20". Nice isn't it?
I've heard of "10 - 10 - 20" results (where the compiler, once it knows 'c'
is const, uses the static data section directly?).

{
 const int c=10;
 int n1=0, n2=0, n3=0;
 int *intPtr=NULL;

 n1 = c;
 intPtr = (int *)&c;
 *intPtr = 20;
 n2 = c;
 n3 = *intPtr;

 printf("\n\n\t%d - %d - %d\n\n", n1, n2, n3);

Quote:
}

    Srgio Ribeiro
    Porto - Portugal


Quote:


> > Hi,
> Hi.

> > It appears the the Microsoft compiler is allowing me to type cast a
const
> > pointer to a non const pointer (See example below).  Is this normal?
Does
> > anyone know of a compilter switch that I can turn on to prevent other
> > programers from updating my const data?

> Yep, it's normal and necessary for compatibility with some old code. Is is
> indeed a very bad idea, but as Stroustrup said : "C++ make it harder to
> shoot oneself in the foot, but when you do it, it blows all your leg away"
> :-)

> What would be good is to have at least a warning. Have you tried compiling
> with Level 4 warning?

> Arnaud
> MVP - VC



Sat, 26 Mar 2005 22:43:50 GMT  
 Type Casting Const Pointers to non const pointers

Quote:

> It appears the the Microsoft compiler is allowing me to type cast a const
> pointer to a non const pointer (See example below).  Is this normal?  Does
> anyone know of a compilter switch that I can turn on to prevent other
> programers from updating my const data?

> #include <stdio.h>

> void Test(const char* const pData)
> {
>     char *pTemp = (char*)pData;

That's a C-style cast.  It's telling the compiler, "I know I can't
normally do this, but I know what I'm doing, so just do it."

You can't stop other programmers from trying to modify your const data,
but if it really is const, it's illegal for them to do so and may
cause their code to break.  If it's not really const, it really is
legal for them to convert it to non-const and modify it.

Quote:
>     *pTemp = 'B';
> }

> void main()
> {
>     const char *pContString = "Hello";

>     Test(pContString);
>     printf(pContString);
> }

See, with this usage here, it should fail with an access violation.
Not only is pContString really const, but also it points to a literal
which should be in a read-only memory space.


Sat, 26 Mar 2005 22:54:49 GMT  
 Type Casting Const Pointers to non const pointers

Quote:
> It appears the the Microsoft compiler is allowing me to type cast a const
> pointer to a non const pointer (See example below).  Is this normal?  Does
> anyone know of a compilter switch that I can turn on to prevent other
> programers from updating my const data?

If you find other programmers attempting to update your const data I suggest
finding a "fire programmer" switch instead of a compiler switch.


Sat, 26 Mar 2005 23:09:44 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. const method and non-const pointer

2. Array of const pointers to const variable.

3. const pointer and const reference

4. problem with array of const pointers to const data

5. const pointer to const data problem

6. get a non-const char pointer to sdt::string

7. const pointer type?

8. const/incompatible pointer type grief

9. Templates with const and non-const parameters

10. Help: inline char const* const& max(char const* const &a, char const* const &b)

11. cast pointer to struct to pointer to type of first member of struct

12. type casting to / from pointers to incomplete data types

 

 
Powered by phpBB® Forum Software