1

This question is regarding the behavior I observed while using const_cast for making a char * const char *. I am aware that this casting is done implicitly and t working for me when the cast is being done implicitly.

The problematic code is:

#include <cstdlib>
int main() {
    const char * org_str = NULL;
    org_str = const_cast<const char*>(getenv("ENV_VAR")); // PROBLEM !!
}

As per the Linux man page getenv() takes const char * and returns char*. So, as per my understanding of const-correctness, I can do a const cast on char* without any issues.

So, my question is, why const_cast here giving me a UB (code is crashing) but as expected without const_cast(implicit casting) its working fine(So the problem has to be with the use of const_cast) ?

Please note, I know implicit cast is the way to go here, through this post I require the answer specifically for the behavior observed here.

EDIT:

Since the bug is non reproducible by fellow So'ers, I am assuming this as some weird runtime/compiler issue. But, do let me know if there is any mention of problems such as this in the standard.

For the time being I am accepting Mike's answer.

7
  • 4
    Are you sure this works at all? Since getenv is a function, this shouldn't compile because you can't cast a function pointer to a const char* by adding or removing const. Similarly, this const_cast is adding const, not removing it, so it should be safe. Can we see your actual code? Commented Feb 8, 2012 at 5:18
  • 2
    Your code is incomplete. sscce.org Commented Feb 8, 2012 at 5:23
  • There is nothing wrong with that code. There must be something wrong with either your compiler, or some other part of your program. Commented Feb 8, 2012 at 7:19
  • Yeah, even I thought it must be elsewhere.But it is working very fine since I removed the const_cast. Commented Feb 8, 2012 at 7:21
  • This code compiles and runs for me on GCC 4.6.2/Linux. Commented Feb 8, 2012 at 8:48

3 Answers 3

3

You are casting the function pointer, not the pointer returned by the function. Call the function first with (), then cast the result.

EDIT: I can't reproduce the problem. Here's the code I used:

#include <cstdlib>
#include <iostream>

using namespace std;
int main() {
    const char * org_str = NULL;
    org_str = const_cast<const char*>(getenv("PATH"));
    cout << "Got: " << org_str << endl;
}

Here's what I got:

$ g++ foo.cc -o foo.app
$ ./foo.app
Got: /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/usr/X11R6/bin
$

BTW, the assignment to NULL is unnecessary; recommended practice is to use one of:

const char *org_str = const_cast<const char*>(getenv("PATH"));

const char *org_str(const_cast<const char*>(getenv("PATH")));

const char *org_str(getenv("PATH"));
Sign up to request clarification or add additional context in comments.

2 Comments

If the OP tried to cast a function pointer with const_cast, it wouldn't even compile. The code posted is not what the OP is actually running.
I have edited the code. Please check, I want to get an env var "ENV_VAR".
2

You don't need a const_cast<> to make something const, you only need it to take away the const-ness.

Also I don't believe the code you have there is correct at all, since getenv is a function and it looks like you're using it as a variable. Perhaps something like this would work:

const char * org_str = getenv("name-of-env");

3 Comments

I know I dont need cons_cast, but whats wrong when I use it, thats the question. Also, I have edited the code.
I tried your sample code in Visual Studio 2010 but nothing went wrong. My suspicion is that something else is going wrong.
@It work for me too when build in debug mode. Its with optimized mode that it is not working.
0

It's, as far as I understand, not the return value of getenv you should cast, but the const char you have. As org_str is constant, you can't assign to it without using const_cast, which means you would need to do something like:

#include <cstdlib>
int main() {
    const char * org_str = NULL;
    const_cast<char*>(org_str) = getenv("ENV_VAR"); // NO PROBLEM !!
}

EDIT: As for having const_cast on the getenv, it makes no sense, as you don't assign to that and therefor will not have any violations of the const expression, as

org_str = getenv("ENV_VAR") will give you.

3 Comments

It's kind of weird, I agree. But this is what is done in: en.cppreference.com/w/cpp/language/const_cast Another use is of course to cast a const object, to change it's values. At least that's the only two use cases I have seen.
I tried this and got: warning: target of assignment not really an lvalue; this will be a hard error in the future.
@martiet: I think lvalue casts can only be used with references. Need to dig the standard though ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.