Unresettable References in C : Understanding the Rationale
In C , references possess unique properties: an immutable connection to a single object and a non-nullable existence. This contrasting nature from pointers, which can be reassigned and annulled, has prompted inquiries about the absence of "non-nullable, reseatable reference or pointer" constructs in C .
The explanation for this design choice lies in Bjarne Stroustrup's "Design and Evolution of C ." Stroustrup's experiences with Algol68 references, where assignments could alter references' targets or re-bind them entirely, shaped his decision. To prevent these potential pitfalls, C references were designed with an unwavering bond to their initial objects.
This design ensures predictability by eliminating the possibility of accidental or deliberate reference re-binding. It guarantees that references consistently point to a valid object, preventing dangling references or the hazards of pointer manipulation. While it restricts flexibility in某些场景中限制了灵活性,这种保守方法避免了潜在的错误和复杂性。
The above is the detailed content of Why Are C References Unresettable?. For more information, please follow other related articles on the PHP Chinese website!