The difference between php references and pointers

王林
Release: 2023-02-24 07:14:02
Original
2894 people have browsed it

The difference between php references and pointers

Essence:

The reference is an alias and the pointer is the address

Specific:

1. Phenomenonally speaking, a pointer can change the value it points to at runtime, but a reference will not change once it is bound to an object. This sentence can be understood as: a pointer can be reassigned to point to a different object. However, the reference always points to the object specified during initialization and cannot be changed later, but the content of the specified object can be changed.

2. From the perspective of memory allocation, the program allocates a memory area for pointer variables but not for references, because the reference must be initialized when it is declared to point to an existing object. References cannot point to null values.

3. From a compilation perspective, the program adds pointers and references to the symbol table respectively during compilation. The symbol table records the variable name and the address corresponding to the variable. The address value corresponding to the pointer variable in the symbol table is the address value of the pointer variable, and the address value corresponding to the reference in the symbol table is the address value of the referenced object.

The symbol table will not be changed after it is generated, so the pointer can change the object pointed to (the value in the pointer variable can be changed), but the reference object cannot be changed. This is the main reason why using pointers is unsafe and using references is safe. In a sense a reference can be thought of as a pointer that cannot be changed.

4. The fact that there is no reference to a null value means that code using references is more efficient than using pointers. Because there is no need to test the validity of a reference before using it. In contrast, pointers should always be tested to prevent them from being null.

5. In theory, there is no limit to the number of pointer levels, but the reference can only be one level. As follows:

int** p1; // 合法。指向指针的指针 int*& p2; // 合法。指向指针的引用 int&* p3; // 非法。指向引用的指针是非法的 int&& p4; // 非法。指向引用的引用是非法的
Copy after login

Attention! The above reading is from left to right.

Recommended tutorial:PHP video tutorial

The above is the detailed content of The difference between php references and pointers. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!