3

According to the c++ grammar, const int* const p means that what p points to and it' value can't be rewritten.But today I found that if I code like this:

void f(const int* const p) { char* ch = (char*) p; int* q = (int*) ch; (*q) = 3; //I can modify the integer that p points to } 

In this condition,the keyword "const" will lose it's effect.Is there any significance to use "const"?

5
  • 6
    If you (C-style) cast you can do everything. Just don't do it ... Commented Nov 28, 2013 at 16:39
  • 1
    Why are you casting away const? Commented Nov 28, 2013 at 16:40
  • 2
    @DavidHeffernan, I think that is what he is trying to understand - I guess he is wondering why C++ allows one to cast the constness away, which juanchopanza explains in his answer :) Commented Nov 28, 2013 at 16:41
  • "I can modify the integer that p points to" - well I think it depends what you pass to the f function, doesnt it? Commented Nov 28, 2013 at 16:41
  • Im wondering why no one has mentioned const_cast yet Commented Nov 28, 2013 at 16:43

5 Answers 5

6

You are casting away the constness here:

char* ch = (char*) p; 

Effectively, you are saying "I know what I am doing, forget you are const, I accept the consequences." C++ allows you to do stuff like this because sometimes it can be useful/necessary. But it is fraught with danger.

Note that if the argument passed to the function were really const, then your code would result in undefined behaviour (UB). And you have no way of knowing from inside the function.

Note also that in C++ it is preferable to make your intent clear,

int* pi = const_cast<int*>(p); 

This makes it clear that your intention is to cast away the const. It is also easier to search for. The same caveats about danger and UB apply.

Sign up to request clarification or add additional context in comments.

3 Comments

to be pedantic, the contract const int* const p simply says that the coder accepts to not introduce any modification via the p pointer to the value pointed by p itself, it doesn't really say that the value pointed by p it's in a safe zone where no one can modify it.
@user2485710 but the function assumes it isn't in a safe zone. If it is, then you get UB.
you can't const_cast int* into char*.
1

Your example will crash the app if const int* const p points to a compile time constant, when casting away constancy you need to be sure what you are doing.

C++ is a language for adults, extra power will never be sacrificed for ephemeral safety, it is the code author's choice whether to stay in a safe zone or to use cast operators and move one step closer to C/asm.

Comments

0

C/C++ will let you do many things that allow you to 'hurt' yourself. Here, casting away the const of p is "legal" because the compiler assumes you know what you are doing. Whether this is good coding style or not is another matter.

When you do something like this, you assume responsibility for any side effects and issues it could create. Here, if the memory pointed to in the parameter is static memory, the program will likely crash.

In short, just because you can do something doesn't mean it is a good idea.

Comments

0

The const keyword is a way to use the compiler to catch programming mistakes, nothing more. It makes no claims about the mutability of memory at runtime, only that the compiler should shout at you if you directly modify the value (without using C-style casts).

A C-style cast is pretty much a marker saying 'I know what I'm doing here'. Which in most instances is false.

Comments

0

Here you change the type of the pointer. Using such a cast (C-type) cast you can change it to any possible pointer with no problem.

The same way you can use c++ cast: const_cast<...>(...):

int* p_non_const = const_cast<int*>(p); 

In this case (I hope) you see immediately what is happening - you simply rid of the constness.

Note that in your program you also don't need temprorary variable ch. You can do it directly:

int* q = (int*) p; 

In principle you should not use such a cast, because correctly designed and written program doesn't need it. Usually the const_cast is used for quick and temporary changes in the program (to check something) or to use a function from some unproperly designed library.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.