How definitively to cast/remove 'constant' ?

How exactly is const_cast<> working which way is for which constant :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main (){

	const char *s="foo bar";

       char *sres;
       char const str[] = "baz";
     
     sres = const_cast<char*>(s);

     cout<<"\nstr result ="<< sres<<"\n";

     sres = const_cast<char*>(str);

     cout<<"\nstr result =="<< sres<<"\n";

}


Gave us in either one
$ ./test
Segmentation fault

Last edited on
There's nothing wrong with this function, as far as I can tell.
Last edited on
I wrote a reply that I deleted, because I assumed too quickly that the string was being modified.
I agree, I don't see the issue.

abdul, can you show a complete program that compiles, and show how you're compiling?

Note that if you actually tried to modify what s, sres, or str points to, then it would be undefined behavior.
Last edited on
const char is a special snowflake, it behaves a little differently as a friendly syntax thing for C and C-strings.

lets take a look at an integer, to be more sure of it all.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
#include<memory.h> //what happened here?  Ill look it up but <cmemory> and <memory> were both rejected on my box. 
using namespace std;


int main()
{
const int n[] = {1,2,3};
int * ip;
unsigned long long derp;
derp = (unsigned long long)(&n[0]);
memcpy(&ip,&derp, sizeof(long long));
ip[0] = 5;
cout << n[0] << endl;  
}



result:
5

basically, the pointer becomes just an integer value for a moment and the const is lost. There are other ways to do it, this is the low level 'take an axe to it' way.

edit: needed long long in online compiler.
Last edited on
Just write
*const_cast<int*>(n) = 5;
so it's easier to identify the problem.

(That program has undefined behavior.)
Last edited on
problem was int wasn't big enough :)
but you are correct, that would help. I still find these casts clunky looking. Old habits.

anyway, it runs here now, and still gives 5, so I guess that will do it.

-- hopefully you only wanted to do this for learning. This is a horrid thing to do, and probably unreliable and unsafe and such.

Last edited on
Your question is meaningless babble.
The program has undefined behavior because it modifies an object declared const.

Using type conversions to sidestep the protections afforded by the type system doesn't eliminate the undefined behavior.
well, of course. you can't answer the question and 'it can't be done' if you want to be THAT way about it lol. But there I am, doing the undoable ;) with an unmitigated hack. It works because undefined is so very often quite predictable, more than anything else.
Add that to the pile of "// THIS WORKS DON'T TOUCH IT" code.

Edit: Funny enough, jonnin's code compiles to
1
2
  mov eax, 5
  ret

with any optimization level turned on.

I wouldn't rely on such behavior for a more complicated function. (The real fun might happen when compiling different translation units, I think that some fun could be had there)
Last edited on
The program has undefined behavior because it modifies an object declared const.

Wouldn't one of the remedies for this be copying the const C string to a non-const C string, using strcpy?

Of course wouldn't the destination string require having enough memory allocated prior to being copied to? Something the OP doesn't do.
Last edited on
It works because undefined is so very often quite predictable

Crashes here
http://coliru.stacked-crooked.com/a/0239d23caf553cec
I wouldn't rely on such behavior for a more complicated function.

I would not either, this sort of stuff is unreliable even across compiler and OS versions on the same platform. Hopefully we all know the snippet was for entertainment only? I am a bit fast and loose on the undefined stuff (I can't even remember a lot of what is and what isn't) and even I would never recommend actually DOING this outside of playing.


Crashes here
I would be amazed if it worked on over 50% of modern platforms, to be honest.

If it crashes on something, double check the pointer vs int type sizes make sure your pointer and int size match. If it still fails after that, would have to dig into why.
Last edited on
Topic archived. No new replies allowed.