Where does the word "dereference" come from?

This question will contain information from project N1570 , so C11 is mainly.

In short, to dereference a pointer means to apply the unary operator * to the pointer. There is only one place where the word “dereferencing” exists in the draft document (there is no instance of “dereferencing”), and this is in the footnote:

102) [...]

Among the invalid values ​​for dereferencing a pointer with a unary * operator is a null pointer, an address incorrectly aligned for the type of object that it points to, and the address of the object after the end of its life

As far as I can see, the unary operator * is actually called the "indirectness operator", as evidenced by §6.5.3.2:

6.5.3.2 Address and Indirection Operators

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

In the general case, it is explicitly called the indirectness operator in Appendix § 1.2:

- Access to the value of the object is carried out by the index of the array [] , member-access . or −> , the address & or an indirect * operator or pointer in creating an address constant (6.6).

Is it right to talk about "dereferencing pointers" in C or is it overly pedantic? Where did the terminology come from? (I can somehow give a pass to [] , called a “delay” due to clause 6.5.2.1)

+6
source share
4 answers

K & R v1

If you look at the C programming language, the first edition (1978) uses the term "indirectness".

Examples

2.12 Priority and order of assessment

[...]

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

7.2 Unary operators

[...]

The unary operator * means an indirect expression: the expression must be a pointer, and the result is an lvalue that refers to the object that the expression points to.

It is also specified in INDEX, for example,

 * indirection operator 89, 187 

Longer Excerpt from Section 5.1

5.1 Pointers and addresses

Since the pointer contains the address of the object, you can access the object "indirectly" through the pointer. Suppose x is a variable, for example int , and that px is a pointer created in some other unspecified way. The unary operator c gives the address of the object, so the statement

 px = &x; 

assigns the address x variable px; px px; px , which is now called "specify" x . The and operator can only be applied to variables and array elements; constructs such as &(x+1 ) and &3 are illegal. It is also illegal to access a variable register.

The unary operator * considers its operand as an address from the final destination and refers to this address to retrieve the contents. so if y is alos a int ,

 y = *px; 

assigns y contents that px points to. So sequence

 px = &x; y = *px; 

assigns the same y value as

 y = x; 

K & R v2

In the second edition, the term "dereferencing" appears.

5.1 Pointers and addresses

The unary operator * is the operator of an indirect or dereference operation; when applied to a pointer, it refers to the object that the pointer points to. Suppose x and y are integers, and ip is a pointer to an int. This artificial sequence shows how to declare a pointer and how to use, and *:

[...]


Previous use

This term, however, is ("much") older, as can be seen, for example,

A Survey of Some Issues Concerning Abstract Data Types , 1974. For example, pp24 / 25. It is said here in connection with ALGOL 68, PASCAL, SIMULA 67.

The mechanism by which pointers are translated into values ​​in a language known as “dereferencing,” a form of coercion (discussed below). Consider the statement

  p := q; 

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

Let '@' be the dereference operator (i.e., if p points to j, then @p matches j) and '#' is the reference operation (i.e. if p points to j, then p is the same as #j). The following table indicates the possible actions that may be required to complete the assignment:

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

[...]


Chasing

There are several other examples of its use. Exactly where and when it was invented, I can not find though (at least for now). (The 1974 article is at least interesting.)


For fun, this can also often be useful for viewing mailing lists such as net.unix-wizards. Example from Peter Lamb at Melbourne Uni (11/28/83):

The dereferencing of NULL pointers is another example of idiots who write “portable” code, assuming, however, that their machine is the only one it will ever run on: the same people who developed cpio with binary headers. Even on VAX, dereferencing NULL will give you trash: of course * (char *) NULL and * (short *) NULL will return you 0, but * (int *) NULL will give you 1024528128 !!!!.

[...]


Ed1. Adding

Not the mention of "dereferencing," but still; Interesting is Richie: C ✝ Language Development

The term "indirectness" is also used here - but / and / etc. the connection between languages ​​is somewhat detailed. The use of this term is thus interesting from the point of view of, for example, such as the 1974 mentioned above.

As an example of indirectness as a concept and syntax, for example, pp. 12 ev.

A syntax accident contributed to the perceived complexity of the language. The indirection operator, written in C, is syntactically a unary prefix operator, 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. Most importantly, C has a relatively rich set of ways to describe types (compared to, say, Pascal). For example, language declarations such as C-Algol 68 describe objects that are difficult to understand, simply because the objects themselves are complex. The second effect is related to syntax details. Statements in C should be read in the inside-out style, which many people find difficult to understand [Anderson 80].


In this regard, ANSI C89 is probably also worth mentioning and mentions:

3.1.2.5 Types

A pointer to void cannot be dereferenced, although such a pointer can be converted to a regular type of pointer that can be dereferenced.

Among the invalid values ​​for dereferencing a pointer by a unary operator * are a null pointer, an address, an incorrectly selected object for the type that the object points to, or the address of an object that has automatic storage time when executing the block in which the declared object is located and all closed blocks are completed.

(Now I need to re-read some of these documents.)

+5
source

Because in the good old days, K & RC language only passed parameters by value. Therefore, pointers were used to simulate pass parameters by reference. And people (wrongly) talked about referencing a variable to build a pointer to a variable.

And dereferencing a pointer was the opposite operation.

C ++ now uses valid references other than pointers, but the word dereferencing is still in use (even if it's not quite right).

+1
source

Kernigan and Richie, C Programming Language, 2nd ed., 5.1:

The unary operator * is the operator of an indirect or dereference operation; [...] '' pointer to void '' is used to store any type of pointer, but cannot be dereferenced by itself.

0
source

I do not know the exact etymology, but you can consider the value of the pointer (in a general sense, not the value of C / C ++) as "referring" to another object in memory; that is, p refers to x . When we use p to get the value stored in x , we bypass this link or de link p .

0
source

Source: https://habr.com/ru/post/979506/


All Articles