当它是有效的访问指针"死亡"目的?

编程入门 行业动态 更新时间:2024-10-27 02:25:35
本文介绍了当它是有效的访问指针"死亡"目的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

首先,要澄清,我的不可以谈论取消引用无效指针!

First, to clarify, I am not talking about dereferencing invalid pointers!

考虑以下两个例子。

示例1

typedef struct { int *p; } T; T a = { malloc(sizeof(int) }; free(a.p); // a.p is now indeterminate? T b = a; // Access through a non-character type?

例2

void foo(int *p) {} int *p = malloc(sizeof(int)); free(p); // p is now indeterminate? foo(p); // Access through a non-character type?

执行下列任一上面的例子未定义行为?

Do either of the above examples invoke undefined behaviour?

上下文

这问题是针对提出到this讨论。该建议是,例如,指针参数可能会被传递给通过86段寄存器功能,这可能导致硬件异常。

This question is posed in response to this discussion. The suggestion was that, for example, pointer arguments may be passed to a function via x86 segment registers, which could cause a hardware exception.

从C99的标准,我们了解以下内容(重点煤矿):

From the C99 standard, we learn the following (emphasis mine):

[3.17] 不确定的值的 - 要么是未指定的值或陷阱重新presentation

[3.17] indeterminate value - either an unspecified value or a trap representation

和则:

[6.2.4 P2] 指针的值变为不确定当  它所指向的对象达到其生命周期的结束。

[6.2.4 p2] The value of a pointer becomes indeterminate when the object it points to reaches the end of its lifetime.

和则:

[6.2.6.1 P5] 某个对象重新presentations不必再present对象类型的值。如果一个对象的存储值有这样一个重新presentation,由左值前pression不具有字符类型,读这种行为是未定义。如果这样的重presentation是由由不具有字符类型左值前pression修改全部或任何对象的一部分副作用产生的,该行为是不确定的。这样的再presentation叫做陷阱重新presentation

[6.2.6.1 p5] Certain object representations need not represent a value of the object type. If the stored value of an object has such a representation and is read by an lvalue expression that does not have character type, the behavior is undefined. If such a representation is produced by a side effect that modifies all or any part of the object by an lvalue expression that does not have character type, the behavior is undefined. Such a representation is called a trap representation.

把所有的这一起,是什么做限制我们对访问指向死的对象?

Taking all of this together, what restrictions do we have on accessing pointers to "dead" objects?

附录

虽然我已经引述上面的C99标准,我很想知道,如果行为在任何C ++标准是不同的。

Whilst I've quoted the C99 standard above, I'd be interested to know if the behaviour differs in any of the C++ standards.

推荐答案

例2是无效的。在你的问题的分析是正确的。

Example 2 is invalid. The analysis in your question is correct.

实施例1是有效的。结构类型从不保存陷阱重新presentation,即便其成员之一呢。这意味着结构分配,一个系统,其中阱再presentations会造成问题时,必须被实现为按字节拷贝,而不是一个部件逐件拷贝。

Example 1 is valid. A structure type never holds a trap representation, even if one of its members does. This means that structure assignment, on a system where trap representations would cause problems, must be implemented as a bytewise copy, rather than a member-by-member copy.

6.2.6重新$类型p $ psentations

6.2.6.1常规

6 [...]结构或联合对象的值是从来没有一件T说唱  再presentation,即使结构或联合对象的成员的值可能  陷阱重新presentation。

6 [...] The value of a structure or union object is never a t rap representation, even though the value of a member of the structure or union object may be a trap representation.

更多推荐

当它是有效的访问指针"死亡"目的?

本文发布于:2023-06-06 06:22:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/539006.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:目的   指针   当它   QUOT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!