Reference Implementation across Compilers and Configurations
References in C are a powerful feature that allow for efficient access to memory beyond the scope of a variable. But how are they actually implemented under the hood?
Standard Recommendations and Implementation Differences
The C standard does not mandate a specific implementation for references. However, compilers generally adhere to certain guidelines:
Example Program and Compiler Output
To demonstrate the internal implementation of references, consider the following program:
#include <stdio.h> #include <stdlib.h> int byref(int &foo) { printf("%d\n", foo); } int byptr(int *foo) { printf("%d\n", *foo); } int main() { int aFoo = 5; byref(aFoo); byptr(&aFoo); }
Compiling this program with LLVM and optimizations disabled produces identical assembly code for both the byref and byptr functions:
define i32 @_Z5byrefRi(i32* %foo) { %foo_addr = alloca i32* ; <i32**> [#uses=2] %retval = alloca i32 ; <i32*> [#uses=1] %"alloca point" = bitcast i32 0 to i32 ; <i32> [#uses=0] store i32* %foo, i32** %foo_addr %0 = load i32** %foo_addr, align 8 ; <i32*> [#uses=1] %1 = load i32* %0, align 4 ; <i32> [#uses=1] %2 = call i32 (i8*, ...)* @printf(i8* noalias getelementptr inbounds ([4 x i8]* @.str, i64 0, i64 0), i32 %1) ; <i32> [#uses=0] br label %return return: ; preds = %entry %retval1 = load i32* %retval ; <i32> [#uses=1] ret i32 %retval1 } define i32 @_Z5byptrPi(i32* %foo) { %foo_addr = alloca i32* ; <i32**> [#uses=2] %retval = alloca i32 ; <i32*> [#uses=1] %"alloca point" = bitcast i32 0 to i32 ; <i32> [#uses=0] store i32* %foo, i32** %foo_addr %0 = load i32** %foo_addr, align 8 ; <i32*> [#uses=1] %1 = load i32* %0, align 4 ; <i32> [#uses=1] %2 = call i32 (i8*, ...)* @printf(i8* noalias getelementptr inbounds ([4 x i8]* @.str, i64 0, i64 0), i32 %1) ; <i32> [#uses=0] br label %return return: ; preds = %entry %retval1 = load i32* %retval ; <i32> [#uses=1] ret i32 %retval1 }
In this assembly code, both functions use identical instructions to load and dereference the input variable foo. This demonstrates that the compiler treats both references and pointers similarly under the hood.
Conclusion
References and pointers are closely related concepts in C . While the standard does not prescribe a specific implementation, compilers generally implement references as pointers. This allows for efficient and interchangeable use of references and pointers for accessing memory beyond the scope of variables.
The above is the detailed content of How Are References Actually Implemented in C ?. For more information, please follow other related articles on the PHP Chinese website!