通过取消引用 NULL 指针来分配引用

发布于 2024-11-25 03:03:19 字数 316 浏览 2 评论 0原文

int&  fun()
{
    int * temp = NULL;
    return *temp;
}

在上面的方法中,我试图取消对 NULL 指针的引用。当我调用这个函数时,它没有给出异常。我发现当返回类型是按引用时,它不会给出异常,如果它是按值返回类型,则它会给出异常。即使将 NULL 指针的取消引用分配给引用(如下面的行),它也不会给出。

int* temp = NULL:
int& temp1 = *temp;

我的问题是,编译器在引用的情况下不会取消引用吗?

int&  fun()
{
    int * temp = NULL;
    return *temp;
}

In the above method, I am trying to do the dereferencing of a NULL pointer. When I call this function it does not give exception. I found when return type is by reference it does not give exception if it is by value then it does. Even when dereferencing of NULL pointer is assinged to reference (like the below line) then also it does not give.

int* temp = NULL:
int& temp1 = *temp;

Here my question is that does not compiler do the dereferencing in case of reference?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

卷耳 2024-12-02 03:03:19

取消引用空指针是未定义行为

未定义的行为意味着任何事情都可能发生,因此不可能为此定义行为。

诚然,我将第 n 次添加这个 C++ 标准引用,但似乎有必要。

关于未定义行为,

C++ 标准第 1.3.24 节规定:

允许的未定义行为范围从完全忽略结果不可预测的情况,到在翻译或程序执行期间以环境特有的记录方式表现(无论是否发出诊断消息),到终止翻译或执行(并发出诊断消息)。

注意:
另外,只是为了引起您的注意:
在函数内使用返回的引用或指向局部变量的指针也是一种未定义行为。您应该使用 new 分配 freestore(heap) 上的指针,然后返回指向它的引用/指针。

编辑:
正如 @James McNellis 在评论中适当指出的那样,
如果未使用返回的指针或引用,则行为是明确定义的

Dereferencing a null pointer is Undefined Behavior.

An Undefined Behavior means anything can happen, So it is not possible to define a behavior for this.

Admittedly, I am going to add this C++ standard quote for the nth time, but seems it needs to be.

Regarding Undefined Behavior,

C++ Standard section 1.3.24 states:

Permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).

NOTE:
Also, just to bring it to your notice:
Using a returned reference or pointer to a local variable inside a function is also an Undefined Behavior. You should be allocating the pointer on freestore(heap) using new and then returning a reference/pointer to it.

EDIT:
As @James McNellis, appropriately points out in the comments,
If the returned pointer or reference is not used, the behavior is well defined.

难如初 2024-12-02 03:03:19

当您取消引用空指针时,不一定会出现异常;所保证的是行为是未定义的(这实际上意味着根本无法保证行为是什么)。

一旦计算了*temp 表达式,就不可能推断出程序的行为。

When you dereference a null pointer, you don't necessarily get an exception; all that is guaranteed is that the behavior is undefined (which really means that there is no guarantee at all as to what the behavior is).

Once the *temp expression is evaluated, it is impossible to reason about the behavior of the program.

半寸时光 2024-12-02 03:03:19

不允许取消引用空指针,因此编译器可以在假设您不这样做的情况下生成代码。如果您无论如何都这样做,编译器可能会很好地告诉您,但它不是必须的。这是合同中您的部分规定您不得这样做。

在这种情况下,我敢打赌,如果您正确设置警告级别,编译器将会很好,并在编译时告诉您问题。

You are not allowed to dereference a null pointer, so the compiler can generate code assuming that you don't do that. If you do it anyway, the compiler might be nice and tell you, but it doesn't have to. It's your part of the contract that says you must not do it.

In this case, I bet the compiler will be nice and tell you the problem already at compile time, if you just set the warning level properly.

三人与歌 2024-12-02 03:03:19

不要 * 空指针,它是 UB。 (未定义的行为,你永远不能假设它会做任何事情,除了点燃你的狗并迫使你拿蘑菇,这将导致精彩的轶事)

Algol/C 系列中空指针的一些历史和信息:http://en.wikipedia.org/wiki/Pointer_(computing)#Null_pointer

未定义行为的示例和含义:http://en.wikipedia.org/wiki/Undefined_behavior#Examples_in_C

Don't * a null pointer, it's UB. (undefined behavior, you can never assume it'll do anything short of lighting your dog on fire and forcing you to take shrooms which will lead to FABULOUS anecdotes)

Some history and information of null pointers in the Algol/C family: http://en.wikipedia.org/wiki/Pointer_(computing)#Null_pointer

Examples and implications of undefined behavior: http://en.wikipedia.org/wiki/Undefined_behavior#Examples_in_C

陌伤ぢ 2024-12-02 03:03:19

我不确定我明白你想做什么。未定义 **NULL** 指针的取消引用。

如果您想表明您的方法并不总是返回值,您可以将其声明为:

bool fun(int &val);

或 stl 方式(类似于 std::map insert):

std::pair<int, bool> fun();

或 boost 方式:

boost::optional<int> fun();

I don't sure I understand what you're trying todo. Dereferencing of ** NULL** pointer is not defined.

In case you want to indicate that you method not always returns value you can declare it as:

bool fun(int &val);

or stl way (similar to std::map insert):

std::pair<int, bool> fun();

or boost way:

boost::optional<int> fun();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文