简体   繁体   English

使用const_cast添加const时的未定义行为?

[英]Undefined behavior when adding a const with const_cast?

This question is regarding the behavior I observed while using const_cast for making a char * const char * . 这个问题与我在使用const_cast制作char * const char *观察到的行为有关。 I am aware that this casting is done implicitly and t working for me when the cast is being done implicitly. 我知道,这铸件含蓄和T为我做工作时,剧组正在隐式进行。

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* . 按照Linux手册页, getenv()接受const char *并返回char* So, as per my understanding of const-correctness, I can do a const cast on char* without any issues. 因此,根据对const正确性的理解,我可以对char*进行const转换,而不会出现任何问题。

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 ) ? 所以,我的问题是,为什么const_cast在这里给我一个UB(代码崩溃),但是按预期的方式在没有const_cast (隐式转换)的情况下可以正常工作(所以问题必须出在使用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. 由于该错误是So'ers同行无法复制的,因此我认为这是一些奇怪的运行时/编译器问题。 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. 我暂时接受迈克的回答。

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; 顺便说一句,分配给NULL是不必要的; 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"));

You don't need a const_cast<> to make something const, you only need it to take away the const-ness. 您不需要const_cast<>即可使const成为某种东西,只需要带走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. 而且我也不相信您那里的代码是完全正确的,因为getenv是一个函数,并且看起来您将其用作变量。 Perhaps something like this would work: 也许这样的事情会起作用:

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

It's, as far as I understand, not the return value of getenv you should cast, but the const char you have. 据我了解,它不是您应该转换的getenv的返回值,而是您拥有的const char。 As org_str is constant, you can't assign to it without using const_cast, which means you would need to do something like: 由于org_str是常量,因此如果不使用const_cast就无法为其分配值,这意味着您需要执行以下操作:

#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 编辑:至于在getenv上具有const_cast,这是没有意义的,因为您没有分配给它,因此不会违反const表达式,因为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM