Questions tagged [pass-by-reference]

Pass by reference is an argument marshalling strategy whereby a variable's location in memory is passed to a function, rather than a copy of the variable's value, although the function appears in the source code to receive the variable itself rather than a pointer to it.

Passing by reference means that the memory address of a variable is passed rather than a copy of the variable's value.

This typically means that the function can modify the passed variable, assigning a new value to it. However for performance reasons, passing by reference may be useful even if the passed structure is not modified, as with the Pascal var modifier, and some programming languages have constructs (like the C const modifier) to disallow modification of a variable passed by reference.

4485 questions
10
votes
2 answers

Why does Clang generate different code for reference and non-null pointer arguments?

This is related to Why can't GCC generate an optimal operator== for a struct of two int32s?. I was playing around with the code from that question at godbolt.org and noticed this odd behavior. struct Point { int x, y; }; bool nonzero_ptr(Point…
benrg
  • 1,395
  • 11
  • 13
10
votes
4 answers

Does Java pass by reference?

Does Java really support passing by reference? If it doesn't, why do we have the == operator for finding two objects with the same reference?
captain
10
votes
7 answers

Pass reference to output location vs using return

Which is better for performance when calling a function that provides a simple datatype -- having it fill in a memory location (passed by pointer) or having it return the simple data? I've oversimplified the example returning a static value of 5…
Paul
  • 5,043
  • 7
  • 24
  • 24
10
votes
5 answers

template pass by value or const reference or...?

I can write a templated function this way template void f(T x) {…} or this way template void f(T const& x) {…} I guess that the second option can be more optimal as it explicitly avoids a copy, but I suspect that it can also fail…
Roman L
  • 3,006
  • 25
  • 37
10
votes
4 answers

How to pass a list by reference?

I'm trying to implement a function 'add' that combines the list L1 with L2 into L3: def add(L1,L2,L3): L3 = L1 + L2 L3 = [] add([1],[0],L3) print L3 The code above produces an empty list as a result instead of [1,0] - This means that L3 wasn't…
user6039980
  • 3,108
  • 8
  • 31
  • 57
10
votes
2 answers

Copy-on-modify semantic on a vector does not append in a loop. Why?

This question sounds to be partially answered here but this is not enough specific to me. I would like to understand better when an object is updated by reference and when it is copied. The simpler example is vector growing. The following code is…
JRR
  • 3,024
  • 2
  • 13
  • 37
10
votes
6 answers

C#'s ref and out in Java

As we know both language are pass-by-value when passing parameters to methods. But C# supports ref and out keywords to pass-by-reference of primitive types. I am looking for the same keywords and technique in Java? My guess is using Integer wrapper…
Tarik
  • 79,711
  • 83
  • 236
  • 349
10
votes
3 answers

How does @_ work in Perl subroutines?

I was always sure that if I pass a Perl subroutine a simple scalar, it can never change its value outside the subroutine. That is: my $x = 100; foo($x); # without knowing anything about foo(), I'm sure $x still == 100 So if I want foo() to change…
David B
  • 29,258
  • 50
  • 133
  • 186
10
votes
2 answers

passing enums by ref in java

How can i pass enum parameter by reference in java? Any solution?
aam
  • 103
  • 1
  • 1
  • 7
10
votes
5 answers

C++ Pass By Const Reference and Return By Const Reference

I'm trying to understand if there is any benefit to returning a const reference. I have a factorial function that normally looks like this: unsigned long factorial(unsigned long n) { return (n == 0) ? 1 : n * factorial(n - 1); } I'm assuming…
Kiril
  • 39,672
  • 31
  • 167
  • 226
10
votes
3 answers

passing ‘const CMyclass’ as ‘this’ argument of ... discards qualifiers [-fpermissive]

compiling the following code via g++ -std=c++11 test.cpp gives me the following error: test.cpp: In lambda function: test.cpp:17:128: error: passing ‘const CMyclass’ as ‘this’ argument of ‘void CMyclass::my_method(const state_type&, double)’…
barej
  • 1,330
  • 3
  • 25
  • 56
10
votes
4 answers

How can I change Integer value when it is an argument like change array's value?

public static void main(String[] args) { Integer i = new Integer(0); int[] arr = {1}; p1(i); p2(arr); System.out.println(i); System.out.println(arr[0]); } public static void p1(Integer i) { i = 2; } public static void…
cow12331
  • 245
  • 2
  • 3
  • 9
10
votes
2 answers

Using rvalue references for default arguments

I want to make a function that takes an optional reference to an object, and creates one for the duration of the function if it is not provided, i.e. void Foo(Bar& b = Bar()) { /* stuff */ } This is, of course, invalid code, as a Bar cannot be…
10
votes
2 answers

Overloading reference vs const reference

I have the following code: #include template void f(T& x) { std::cout << "f(T& )" << std::endl; } template void f(const T& x) { std::cout << "f(const T& )" << std::endl; } int main() { …
vsoftco
  • 55,410
  • 12
  • 139
  • 252
10
votes
4 answers

Why are objects automatically passed by reference?

I have a general question about deep- and shallow-copy in the context of the pass-by-reference- and pass-by-value-concept of C#: In C# it is a requirement to explicitly create methods that accept pointers/references to be able to pass such to the…
Florian R. Klein
  • 1,375
  • 2
  • 15
  • 32