c - "dereferencing"这个词是从哪里来的?

标签 c pointers language-lawyer nomenclature

本题将从草稿N1570中获取信息,所以基本上是C11。

通俗地说,取消引用指针意味着将一元 * 运算符应用于指针。草稿文件中只有一处存在“取消引用”一词(没有“取消引用”的实例),并且位于脚注中:

102) [...]

Among the invalid values for dereferencing a pointer by the unary * operator are a null pointer, an address inappropriately aligned for the type of object pointed to, and the address of an object after the end of its lifetime

据我所知,一元 * 运算符实际上称为“间接运算符”,如第 §6.5.3.2 节所示:

6.5.3.2 Address and indirection operators

4 The unary * operator denotes indirection. [...]

同样,它在附件§J.2中被明确称为间接运算符:

— The value of an object is accessed by an array-subscript [], member-access . or −>, address &, or indirection * operator or a pointer cast in creating an address constant (6.6).

那么在 C 中谈论“取消引用指针”是否正确,或者这是否过于迂腐?术语从何而来? (由于 §6.5.2.1,我可以同意 [] 被称为“尊重”)

最佳答案

K&R v1

如果你看一下C 编程语言,第一版(1978 年),就会使用术语“间接”

示例

2.12 Precedence and Order of Evaluation

[…]

Chapter 5 discusses * (indirection) and & (address of).

7.2 Unary operators

[…]

The unary * operator means indirection: the expression must be a pointer, and the result is an lvalue referring to the object to which the expression points.

它也在 INDEX 中列出,例如

* indirection operator 89, 187

第 5.1 节的较长摘录

5.1 Pointers and Addresses

      Since a pointer contains the address of an object, it is possible to access the object “indirectly” through the pointer. Suppose that x is a variable, say an int, and that px is a pointer, created in some as yet unspecified way. The unary operator c gives the address of an object, so the statement

px = &x;

assigns the address of x to the variable px; px is now said to “point to” x. The & operator can be applied only to variables and array elements; constructs like &(x+1 ) and &3 are illegal. It is also illegal to take the address of a register variable.

    The unary operator * treats its operand as the address off the ultimate target, and accesses that address to fetch the contents. Thus if y is alos an int,

y = *px;

assigns to y the contents of whatever px points to. So the sequence

px = &x;
y = *px;

assigns the same value to y as does

y = x;

K&R v2

在第二版中出现了术语“取消引用”。

5.1 Pointers and Addresses

The unary operator * is the indirection or dereferencing operator; when applied to a pointer, it accesses the object the pointer points to. Suppose that x and y are integers and ip is a pointer to int. This artificial sequence shows how to declare a pointer and how to use & and *:

[…]

<小时/>

之前的使用情况

然而,这个术语(“远”)更古老,例如:

A survey of some issues concerning abstract data types ,1974 年。例如第 24/25 页。这里结合ALGOL 68、PASCAL、SIMULA 67进行阐述。

The mechanism by which pointers are transformed into values by a language is known as 'dereferencing', a form of coercion (discussed later). Consider the statement

 p := q;

Depending upon the types of p and q, there are several possible interpretations.

Let '@' be a dereferencing operator (i.e. if p points to j , then @p is the same as j) and '#' be a referencing operation (i.e. if p points to j , then p is the same as #j). The following table indicates the possible actions a language might take to perform the assignment:

                       |                                         
                       |   type of p                             
                       |                                         
                       |   t         ref t     ref ref t . . .   
                       |                                         
        ---------------------------------------------------------
                       |                                         
           t           |  p←q        p←#q       p←##q            
                       |             @p←q       @p←#q            
                       |                        @@p←q            
type                   |                                         
of                     |                                         
q          ref t       |  p←@q       p←q        p←#q             
                       |             @p←@q      @p←q             
                       |                        @@p←@q           
                       |                                         
                       |                                         
           ref ref t   |  p←@@q      p←@q       p←q              
             .         |             @p←@@q     @p←@q            
             .         |                        @@p←@@q          
             .         |                                         
                       |                                         
                       |                                         

[…]

<小时/>

类型转换

还有几个其他的用法示例。但我无法找到它的确切创造地点和时间(至少现在还没有)。 (1974 年的论文至少很有趣。)

<小时/>

为了获得乐趣,查看诸如 net.unix-wizards 之类的邮件列表通常也很有用。安example from Peter Lamb at Melbourne Uni (11/28/83):

Dereferencing NULL pointers is yet another example of idiots who write 'portable' code, assuming however, that THEIR machine is the only one on which it will ever run: the same sorts of people who designed cpio with binary headers. Even on a VAX, dereferencing NULL will get you garbage: sure, *(char *)NULL and *(short *)NULL return you 0, but *(int *)NULL will give you 1024528128 !!!!.

[…]

<小时/>

Ed1。添加

没有提到“取消引用”,但仍然; Ritchie 的一个有趣的读物是:The Development of the C Language ✝

这里也一直使用术语“间接”——但是/和/等等。语言之间的联系有些详细。因此,考虑到例如,该术语的使用很有趣。像上面提到的 1974 年的那篇论文一样。

作为间接概念和语法的示例,例如:第 12 页(EV)。

    An accident of syntax contributed to the perceived complexity of the language. The indirection operator, spelled * in C, is syntactically a unary prefix operator, just as in BCPL and B. This works well in simple expressions, but in more complex cases, parentheses are required to direct the parsing.

[…]

There are two effects occurring. Most important, C has a relatively rich set of ways of describing types (compared, say, with Pascal). Declarations in languages as expressive as C– Algol 68, for example – describe objects equally hard to understand, simply because the objects themselves are complex. A second effect owes to details of the syntax. Declarations in C must be read in an ‘inside-out’ style that many find difficult to grasp [Anderson 80].

<小时/>

在这方面,很可能还值得一提 ANSI C89 并提及如下:

3.1.2.5 Types

A pointer to void may not be dereferenced, although such a pointer may be converted to a normal pointer type which may be dereferenced.

Among the invalid values for dereferencing a pointer by the unary * operator are a null pointer, an address inappropriately aligned for the type of object pointed to, or the address of an object that has automatic storage duration when execution of the block in which the object is declared and of all enclosed blocks has terminated.

(我现在必须重新阅读其中一些文档。)

关于c - "dereferencing"这个词是从哪里来的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29473557/

相关文章:

c - 使用 goto 语句强制执行至少一次 for 循环迭代的有效性

c++ - std::string (*)[96][60] 和 std::string* [96][60] 有什么区别?

c++ - LPCSTR 没有 'long' 并且 UINT_PTR 没有指针?

c++ - 为什么这些具有外部链接的名称不表示同一实体?

c++ - 为什么在没有声明的情况下不从 .cpp 文件中获取模板函数的完全特化?

c++ - 类成员 `B` 的析构函数,为什么在下面的代码片段中调用它?

c++ - 发送或接收结构时的字节对齐

信用程序无法编译

c - 如何调试程序挂起的原因?

c++ - vector::erase with pointer 成员