Home > Backend Development > C++ > body text

How Are References Actually Implemented in C ?

Susan Sarandon
Release: 2024-11-26 09:02:14
Original
172 people have browsed it

How Are References Actually Implemented in C  ?

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:

  • References are typically implemented as pointers to the referenced object.
  • References can refer to both local and global variables.
  • Pointers and references are often interchangeable.

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 &amp;foo) { printf("%d\n", foo); }
int byptr(int *foo) { printf("%d\n", *foo); }

int main() {
  int aFoo = 5;
  byref(aFoo);
  byptr(&amp;aFoo);
}
Copy after login

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
}
Copy after login

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!

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 Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template