Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

c++ - What's the low-level difference between a pointer an a reference?

If we have this code:

int foo=100;
int& reference = foo;
int* pointer = &reference;

There's no actual binary difference in the reference's data and the pointer's data. (they both contain the location in memory of foo)

part 2

So where do all the other differences between pointers and references (discussed here) come in? Does the compiler enforce them or are they actually different types of variables on the assemebly level? In other words, do the following produce the same assembly language?

foo=100;
int& reference=foo;
reference=5;

foo=100;
int* pointer=&foo;
*pointer=5;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Theoretically, they could be implemented in different ways.

In practice, every compiler I've seen compiles pointers and references to the same machine code. The distinction is entirely at the language level.

But, like cdiggins says, you shouldn't depend on that generalization until you've verified it's true for your compiler and platform.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...