What is your deep understanding of pointer, reference, and descriptor in C, C ++, and Java?

What is your deep understanding of pointer, reference, and descriptor in C, C ++, and Java?

We usually think of a pointer, a link, and a descriptor at a given level of the language, it is easy to introduce confusion to a novice like me.

Actually all these concepts in java, just an encapsulation of a pointer. All pointers simply encapsulate main memory addresses. So all this, just encapsulation.

all above is my personal opinion. And what is your understanding?

Welcome to share with me.

+4
source share
2 answers

Each language has differences in this regard. In C, there are only pointers, which are variables containing a memory address. In C, you can use pointer arithmetic to move through memory, if you have an array, you can get a pointer to the first element and move through memory by increasing the pointer.

Java references are similar to pointers in that they refer to a location in memory, but you cannot use the arithmetic of pointers to them. Only appointments are allowed. Please note that the link is not an object, but a way to access the object. This can be seen in the semantics of passing arguments: objects are not passed by reference, links are passed by value:

public static void swap( Object o1, Object o2 ) { Object tmp = o1; o1 = o2; o2 = tmp; } 

The previous code snippet is complicated no-op. References to two objects are passed by value, they are reproduced inside the method, and nothing happens from the point of view of the caller: real objects are not subject to any changes, as well as links to calling objects in these objects. That is, if the call is swap (ref1, ref2), the system will make copies of the links in o1 and o2, the copies will be changed within the method, but the variables caller ref1 and ref2 will remain unchanged after the method is called.

In C ++, you have two concepts: pointers match C pointers and are close to Java links, and C ++ links are aliases to the objects that they reference. C ++ links can only be initialized with a single object / data element in the structure and from there using the original object, and the link is exactly the same. Besides the fact that the links do not contain a resource, and thus, the destructor will not be called when the link goes out of scope, and there will also be no link to the notification if the mentioned object is destroyed, for all other purposes, the two names are one and the same element.

 template <typename T> void swap( T & a, T & b ) { T tmp( a ); a = b; b = tmp; } 

The code above in C ++ differs from the Java version in that it modifies caller objects. If the caller uses swap (var1, var2), then the links are bound to these variables, and var1 and var2 are those that suffer from the change. After the call, the values ​​of var1 and var2 are actually replaced.

Pens are on a different level, they are not a language construct, but tokens provided by the library, so that later you can refer to some resource that the library manages internally. The most common case is whole descriptors, which are identifiers (or offsets) in the resource table, but I saw rows used as descriptors. This is an internal library that decides what exactly is a handler (pointer, integer, string or more complex data structure). Pens should be opaque in the sense that the only reasonable use is to store them and then return them to the same library as part of other function signatures.

+8
source

In C ++, a pointer is a variable that points to a location in memory. You can access the object or data stored there by dereferencing the pointer. A link is just a pointer that has two differences from a pointer. First, you cannot change what the link refers to after the link is initialized. Secondly, the dereferencing semantics are removed, so you can access the link as if it were an object allocated on the stack, and not in a bunch with a new one.

There are no pointers in Java, only links. Each object that you use is a reference to an object allocated on the heap. The downside is that you cannot do math tricks. It is also up.

EDIT: As stated in the comments, the Java link differs from the C ++ link in that it can be reassigned after initialization. They are still called "reference types" according to the language specification, but behaviorally they act as pointers in terms of the possibility of reassigning and passing functions, but the semantics of dereferencing them look like non-indicative access in C ++.

+1
source

All Articles