简体   繁体   English

按值传递与按引用传递内存分配差异C ++

[英]Pass by value vs pass by reference memory allocation difference c++

I want to pass a huge stl vector into a function in C++, when I do pass by value, does it make a copy of the huge vector internally? 我想将巨大的stl向量传递给C ++中的函数,当我按值传递时,它是否在内部复制了巨大的向量? I'm not clear how pass by value and pass by reference differ in terms of memory allocation. 我不清楚按值传递和按引用传递在内存分配方面如何不同。 Is pass-by-reference more memory efficient? 通过引用传递的内存效率更高吗? Should I pass just a pointer of that huge vector into the function so that I save a lot of memory? 我是否应该仅将那个巨大向量的指针传递给函数,以便节省大量内存?

Could someone explain to me the difference in terms of memory for the following three cases? 有人可以向我解释以下三种情况在记忆方面的区别吗? Assume obj is huge. 假设obj很大。

1. func(vector<obj> )

2. func(vector<obj>*) 

3. func(vector<obj*>)

Your third option is totally different than the first two. 您的第三个选项与前两个选项完全不同。 The type is different. 类型不同。 I'm going to talk about the first two options (and present some more). 我将讨论前两个选项(并提供更多选项)。

func(vector<obj> ) - pass by value func(vector<obj> ) - 按值传递

Changes made internally are not reflected on the outside. 内部所做的更改不会反映在外部。 Theoretically, a copy is made, yes. 从理论上讲,是复制的。 However, the semantics are different. 但是, 语义是不同的。 You couldn't pass by value and achieve the same thing. 您无法通过价值来实现相同的目标。 Also, with move semantics in C++11, a situation where a pass-by-value and pass-by-reference behave the same has the same efficiency, because the vector isn't copied, but moved . 同样,对于C ++ 11中的move语义,值传递和引用传递行为相同的情况具有相同的效率,因为矢量不是被复制而是被移动了

func(vector<obj>*) - pass a pointer by value func(vector<obj>*) - 按值传递指针

A copy of the pointer is made. 指针的副本。 Internally, changes to the pointer itself aren't reflected on the outside. 在内部,对指针本身的更改不会反映在外部。 Changes to the vector it points to are. 它指向的vector更改。 So the effect is different from the option before - if you want to make changes that aren't reflected on the outside, you need a copy anyways. 因此,效果与之前的选项不同-如果您要进行外部未反映的更改,则无论如何都需要一个副本。

func(/*const*/ vector<obj>&) - pass by reference func(/*const*/ vector<obj>&) - 通过引用传递

If not const , changes are reflected on the outside. 如果不是const ,则更改将反映在外部。 Again, different semantics - if you're modifying the object, changes will be reflected on the outside. 同样,不同的语义-如果您要修改对象,则更改将反映在外部。 If you don't want that (changes reflected on the outside), you need a copy anyway. 如果您不希望这样做(更改反映在外部),则仍然需要一份副本。

The decision between the three isn't trivial. 三者之间的决定并非无关紧要。

The second one is closer to what you'd do in C, so I'd avoid it if I could (not needed + you can achieve the same effect with pass-by-reference + no naked pointers). 第二个更接近于您在C语言中所做的事情,因此,如果可以的话,我会避免使用它(不需要+您可以通过引用传递+没有裸指针来达到相同的效果)。

If you want to modify the object inside the function and have the changes reflected on the outside, use pass by non- const reference. 如果你想修改函数内部的对象,并有变化反映在外面,使用路过的非const参考。 This couldn't be achieved by pass-by-value anyway. 无论如何,这无法通过值传递来实现。

If you don't plan on modifying the object, use pass by const reference. 如果您不打算修改该对象,请使用const引用传递。 This guarantees no copy is made, and that you're not modifying the object. 这样可以确保不进行任何复制,并且您不会修改对象。

If you want to modify the object but not have the changes reflected on the outside, use pass-by-value. 如果要修改对象,但不希望外部反映更改,请使用值传递。 In situations where changes wouldn't be reflected on the outside, a copy is avoided (ie when you pass a temporary) and a move is preferred. 如果更改不会在外部反映出来,则应避免使用副本(例如,当您通过临时文件夹时),因此首选移动。 In situations where they would, you'd need a copy of the original object anyway. 在可能的情况下,您仍然需要原始对象的副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM