+ 4
Pass by reference or pass by pointer?
We can pass a vector to a function to change its actual members by two ways: 1: void func(vector<int>*ptr) ... func(&vec); 2: void func(vector<int>& vec) ... func(vec); But what's the difference between the two? Which one shall I choose and which one is better/faster?
23 Answers
+ 4
Both passing a pointer and passing a reference allow you to modify the contents of a vector in a function, but they have some differences in terms of syntax and behavior.
When you pass a pointer to a function, you are passing the memory address of the vector, and the function can modify the vector through this address. This requires the use of the dereferencing operator "*" to access the vector elements. For example, if you want to access the first element of the vector, you would use (*ptr)[0]. This can be less intuitive and more error-prone, especially for beginners.
When you pass a reference to a function, you are essentially passing an alias of the vector, and the function can modify the vector directly using normal dot notation. For example, if you want to access the first element of the vector, you would use vec[0]. This syntax is usually considered more natural and less error-prone.
In terms of performance, passing a reference is usually faster than passing a pointer because there is no need to dereference the pointer. However, the performance difference is usually negligible for small vectors.
In general, if you are not dealing with legacy code or special cases, it is recommended to pass a reference to modify the contents of a vector in a function, as it is more intuitive and less error-prone.
+ 3
Ali_combination
I like this phrase:
"In C++ use references when you can, and pointers when you have to."
The replies pretty much have everything covered. But if you're up to reading more about pointers vs reference:
https://www.geeksforgeeks.org/pointers-vs-references-cpp/amp/
+ 2
ıllıllı ѕнαικн нαмzα ıllıllı i agree on it 👍
+ 2
Pass by reference: This is a way of calling function in which we pass in function arguments values .
* Another thing is that it's used when we don't want to change in global variables only function perform with local variables.
Pass by pointer : In this , as a function argument, we pass the addresses of global variables .
* In this, we need to change our global variables so we pass global variables addresses .
As an example, we created a function for swap two no function, and then we need to Pass it's global variables addresses, not variables reference (values ) otherwise in function local variables will be swap but when we print it in main then it's normal as like before 😏
+ 1
Ani Jona 🕊 I see ok.
+ 1
Mirielle great 👍 thank you.
+ 1
Ani Jona 🕊 thank you also for sharing the book (Optimization software in C++). It may be a good book to read.
+ 1
Ani Jona 🕊 I'm not much familiar with assembly and how C/C++ work, are pointers saved in CPU registry ? (and not stack or heap ?)
+ 1
Ani Jona 🕊 ok, im asking because i couldn't exactly understand what you were saying up there in your last comment.
+ 1
Okay friends, I see different responses and I may need to pump up my knowledge if I want to understand them well. Thank you very much for taking the time to help me out.
+ 1
Salam Aleykum bratvalar
+ 1
emil q-yevv Hello Emil. Please read rules of Sololearn forum. Getting away from the main topic is not the right thing to do, stick to it please. 👍
+ 1
answering the main question, i'd like to add that the key difference (as i think) is that pointers can be null, can be reassigned to another object, while references can only "point at" one existing object. there are also smart pointers, which manage their lifetime, so pointers usage is more about allocating/deallocating memory, for example, than just simply passing something as an argument
also, what i haven't seen mentioned is that there are different kind of references, like these: const T&, T&&, T& (T is some type), they mean different things - read about move semantics, lvalues and rvalues stuff. for some unknown reason there is no mentioning of them on sololearn c++ course (i haven't seen any), while it is such an important concept
0
Ani Jona 🕊 Mirielle
Ok so by now, at least 'passing by reference' seems to be unanimous to be used, but given proofs of that may differ. I may have have to ponder over it to determine the truth (however I am not working on a big project ... I would just like to know it, only for the sake of my interest in C++/C), and to do this, I need to aquire some knowledge through these books you both introduced. Thank you again for your comments. Best of luck.
0
i dont understand where is "copying by passing through pointers"? if you don't call copy constructor or use assignment operator, of course