当我解引用一个指针以通过引用传递它时,编译器级别会发生什么?

huangapple go评论99阅读模式
英文:

What happens at the compiler level when I dereference a pointer to pass it by reference?

问题

I'm interested in what the compiler does when I dereference a pointer explicitly to pass it by reference:

  1. void foo(A& arg)
  2. {
  3. arg.member = 7;
  4. }
  5. void goo()
  6. {
  7. A* ob = new A();
  8. foo(*ob);
  9. printf(ob->member); // should show 7
  10. }

The reason I'm interested is because I believe bringing the dereference out of the function call would create very different behavior:

  1. void foo(A& arg)
  2. {
  3. arg.member = 7;
  4. }
  5. void goo()
  6. {
  7. A* ob = new A();
  8. A ob_dereferenced = *ob;
  9. foo(ob_dereferenced);
  10. printf(ob->member); // should show whatever A initializes member to
  11. }
英文:

I'm interested in what the compiler does when I dereference a pointer explicitly to pass it by reference:

  1. void foo(A& arg)
  2. {
  3. arg.member = 7;
  4. }
  5. void goo()
  6. {
  7. A* ob = new A();
  8. foo(*ob);
  9. printf(ob->member); // should show 7
  10. }

The reason I'm interested is because I believe bringing the dereference out of the function call would create very different behavour:

  1. void foo(A& arg)
  2. {
  3. arg.member = 7;
  4. }
  5. void goo()
  6. {
  7. A* ob = new A();
  8. A ob_dereferenced = *ob;
  9. foo(ob_dereferenced);
  10. printf(ob->member); // should show whatever A initialises member to
  11. }

答案1

得分: 1

你的比较并不公平,因为在第二个版本中:

  1. void goo()
  2. {
  3. A* ob = new A();
  4. A ob_dereferenced = *ob; // <---- here !!!
  5. foo(ob->member); // should show whatever A initialises member to
  6. }

你正在复制 A 实例。根据 A 是什么,创建第二个实例可能会产生可观察到的副作用。

然而,让我们暂时不考虑这种可观察到的副作用,并使用

  1. struct A { int member = 0; };

那么这两种方式之间没有可观察到的差异:

  1. void moo(A* a) {
  2. foo(*a);
  3. }

和使用本地引用:

  1. void moo(A* a) {
  2. A& b = *a;
  3. foo(b);
  4. }

这两个 moo 的作用是相同的。在启用优化时,不应该期望编译器产生不同的输出。

PS:你两个版本都存在内存泄漏问题。如果你只是需要一个指针作为示例,你不需要使用 new。例如,A a; A* aptr = &a; 完全可以用作指向 A 的合法指针,你不需要担心内存泄漏问题。上面我避免了这个问题,只是假设指针来自某个地方

英文:

Your comparison is not a fair one because here in the second version:

> void goo()
> {
> A* ob = new A();
> A ob_dereferenced = *ob; // <---- here !!!
> foo(ob_dereferenced);
> printf(ob->member); // should show whatever A initialises member to
> }

You are making a copy of the A instance. Depending on what A is, creating a second instance can have observable side effects.

However, lets put such observable side effects aside, and use

  1. struct A { int member = 0; };

then there is no observable difference between:

  1. void moo(A* a) {
  2. foo(*a);
  3. }

and using a local reference:

  1. void moo(A* a) {
  2. A&amp; b = *a;
  3. foo(b);
  4. }

Those two moo do the same. There is no reason to expect different output from the compiler when optimizations are turned on.

PS: Your both versions leak memory. If you need a pointer for an example you do not need new. For example A a; A* aptr = &amp;a; makes up for a totally fine pointer to A and you dont need to worry about leaks. Above I avoided the issue, by simply assuming the pointer comes from somewhere.

huangapple
  • 本文由 发表于 2023年6月26日 19:47:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76556383.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定