按值传递与按引用传递(两者内存空间分配的差异) [英] Pass by value vs Pass by reference(difference in space allocation of memory between the two)

查看:74
本文介绍了按值传递与按引用传递(两者内存空间分配的差异)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C++ 中,我们使用通过引用传递,我们引用从参数传递给函数参数的任何地址,它本质上是一个指针,对吗?因此,虽然它们本质上是相同的东西,别名等等,但指针是否也需要内存空间?因此,我们在参数函数中拥有的任何内容都不应该让我们调用 B 指向传递的任何参数的内存位置让我们调用 A,它反过来是我们的值的内存位置(因为 A 传递了我们的值的内存位置作为论点)?

在我们使用按值传递的 Java 中,我们复制了我们传递的任何内容的地址(例如对对象的引用).

所以最后我并没有真正看到按值传递和按引用传递之间的区别.按值传递为原始传递的参数在内存中分配空间,同时指向值和按引用传递的副本将我们值的内存位置作为参数(在内存中分配空间的指针)作为参数传递.函数用于指向值.

解决方案

在 C++ 中,我们使用通过引用传递,我们引用我们传递的任何内容的地址从实参到函数的形参本质上是一个指针吧?

没有.引用是现有变量的别名(即替代名称).
但是在程序集级别,您的实现可能会将引用变量的地址放在地址寄存器(或类似的东西)中,以供被调用函数使用(如果这就是您的意思).

但为了简单起见,您可以将其视为自动取消引用的指针(这就是我刚开始时所做的).但是当你进入语言时,引用实际上与指针有着根本的不同.

<块引用>

所以虽然它们本质上是相同的东西,别名等等,但指针不是也需要内存空间吗?

C++ 级别的指针需要空间(因为它是可寻址的).您可以获取指针的地址.从根本上说,参考文献不需要空格(因为您无法获取其地址).在实现级别,它可能有也可能没有物理内存位置,具体取决于编译器的实现方式.

<块引用>

所以我们在参数函数中的任何东西都不应该让我们调用 B 指向传递的任何参数的内存位置让我们调用 A

如果您用代码示例解释了上述内容,那就太好了.但我想我明白了.假设函数没有内联,那么作为引用传递的任何参数都需要某种形式的链接回原始对象(因为引用总是从根本上引用活动对象).那么它是怎么做的.编译器实现细节(所以你不应该关心).但可能是堆栈上的一个指针,或者只是地址寄存器中的一个地址.

<块引用>

这又是我们值的内存位置(因为 A 传递了我们值的内存位置作为参数)?

也许与否.参考文献在语言级别没有物理位置.所以编译器可以玩很多漂亮的小技巧.

<块引用>

在我们使用按值传递的 Java 中,我们复制了我们传递的任何内容的地址(例如对对象的引用).

在 Java 中,您按值传递引用.但是 Java 引用基本上只是指向内存位置的指针.所以你是按值传递一个指针.这是一种可以使用的技术.幸运的是,C++ 不会将您限制在一种技术上.

您可以通过值或引用传递参数.您甚至可以通过值或引用传递指向对象的指针.因此,可以根据情况使用一些有趣的技术.

<块引用>

所以最后我并没有真正看到按值传递和按引用传递之间的区别.

也许这是因为您正在考虑 Java 引用(按值传递).

在 C++ 中.如果您按值传递,您将创建一个作为参数传递的新对象(这意味着您制作原始对象的副本,这可能会产生成本).如果通过引用传递,则是将别名传递给对象.因此,当您与对象交互时,您正在修改原始对象.

 int inc(int val)//按值传递{返回 ++val;//增加传递的值并作为结果返回.}int incref(int& val)//通过引用传递{返回 ++val;//增加引用.//由于引用是别名,所以这个增量会影响//原始对象.结果被返回.}无效代码(){整数 x = 5;int y = inc(x);//x = 5 和 y = 6int a = 8;int b = incref(a);//a = 9 和 b = 9}

<块引用>

按值传递为原始传递的参数分配内存空间,同时指向值和按引用传递的副本将我们值的内存位置作为参数传递给参数(分配内存空间的指针)) 在我们的函数中用于指向值.

对不起,我弄丢了.

In C++ where we use pass by reference we reference the address of whatever it is that we passed from the argument to the parameter of the function which is essentially a pointer right? So while they are essentially the same thing, alias and all, doesnt a pointer require memory space as well? So shouldnt whatever we have in a parameter function let us call B point to the memory location of whatever the argument was that was passed let us call A which in turn is the memory location of our value( since A passed the memory location of our value as the argument)?

In java where we use pass by value we make a copy of the address of whatever it was we passed(the reference to the object for example).

So in the end i'm not truely seeing the difference between pass by value and pass by reference. Pass by value allocates space in memory for the original passed argument and the copy that both point to the value and pass by reference passes the memory location of our value as the argument which the parameter(the pointer which allocates space in the memory ) in our function uses to point to the value.

解决方案

In C++ where we use pass by reference we reference the address of whatever it is that we passed from the argument to the parameter of the function which is essentially a pointer right?

No. A reference is an alias (ie an alternative name) for an existing variable.
But at the assembly level your implementation may put the address of the referenced variable in an address register (or something similar) for the called function to use (if that is what you mean).

But for simplification you can think of it as a pointer that is automatically de-referenced (that is what I did when I first started). But when you get into the language references are actually fundamentally different from pointers.

So while they are essentially the same thing, alias and all, doesnt a pointer require memory space as well?

A pointer at the C++ level requies space (as it is addressable). You can take the address of a pointer. Fundamentally a reference does not require space (as you can't take its address). At an implementation level it may or may not have a physical memory location depending on how the compiler is implementing it.

So shouldnt whatever we have in a parameter function let us call B point to the memory location of whatever the argument was that was passed let us call A

It would have been nice if you had explained the above with a code example. But I think I understand. Assuming the function is not inlined then any parameter passed as reference needs some form of link back to the original object (as references always fundamentally refer to live objects). So how does it do. Compiler implementation detail (so you should not care). But probably a pointer on the stack or maybe just an address in an address register.

which in turn is the memory location of our value( since A passed the memory location of our value as the argument)?

Maybe or not. References do not have a physical location at the language level. So the compiler can play lots of nice little tricks with that.

In java where we use pass by value we make a copy of the address of whatever it was we passed(the reference to the object for example).

In Java you pass references by value. But Java references are fundamentally just pointers at a memory location. So you are passing a pointer by value. Which is one technique to use. Luckily C++ does not limit you to a single technique.

You can pass parameters by value or reference. You can even pass a pointer to an object by value or reference. So a couple of interesting techniques to use depending on the situation.

So in the end i'm not truely seeing the difference between pass by value and pass by reference.

Maybe that is because you are thinking about java references (which are passed by value).

In C++. If you pass by value, you are creating a new object that is passed as the parameter (which means you make a copy of the original, which can cost). If you pass by reference you are passing an alias to an object. So when you interact with the object you are modifying the original object.

 int inc(int val)     // pass by value
 {
     return ++val;    // increment the passed value and return as a result.
 }

 int incref(int& val) // pass by reference
 {
     return ++val;    // increment the reference.
                      // Since the reference is an alias this increment affects the
                      // original object. The result is returned.
 }

 void code()
 {
       int x = 5;
       int y = inc(x);  // x =5 and y = 6

       int a = 8;
       int b = incref(a); // a = 9 and b = 9
 }  

Pass by value allocates space in memory for the original passed argument and the copy that both point to the value and pass by reference passes the memory location of our value as the argument which the parameter(the pointer which allocates space in the memory ) in our function uses to point to the value.

Sorry I lost that.

这篇关于按值传递与按引用传递(两者内存空间分配的差异)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆