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
8
votes
3 answers

Taking address of temporary - workaround needed

I am facing a GCC warning that I want to fix. Basically I am passing to a method a pointer to a local variable, which in my case is perfectly OK. I understand why the compiler tells me that this is a potential problem, but in my case this is OK. How…
elcuco
  • 8,948
  • 9
  • 47
  • 69
8
votes
5 answers

Detect infinite recursion?

Lets assume I have a function that crawls over an array... flatten([a, b, c, d, [e, f, g, [h, i, j, k], l], m, n, o, p]) >> [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p] Flatten would crawl over the code and for each array encountered would…
Incognito
  • 20,537
  • 15
  • 80
  • 120
8
votes
2 answers

Second order functions in GLSL?

I'm looking for a way to use a function as an argument to another function in GLSL. In regular C, it can be simulated by passing a function pointer as a function argument. It also seems that other languages (like HLSL) now provide ways to deal with…
8
votes
4 answers

Object by Reference vs. Reference by Value

I read this comment here: Passing a String by Reference in Java? Yes, it's a misconception. It's a huge, widespread misconception. It leads to an interview question I hate: ("how does Java pass arguments"). I hate it because roughly half of the…
Steve the Maker
  • 611
  • 6
  • 11
8
votes
2 answers

why is "error:&error" used here (objective-c)

why is "error:&error" used here (objective-c) NSError *error = nil; NSArray *array = [moc executeFetchRequest:request error:&error]; wouldn't an object in objective-c be effectively pass-by-reference anyway?
Greg
  • 34,042
  • 79
  • 253
  • 454
8
votes
4 answers

Immutable and pass by value

I have the following code which has a mutable Person class, String and a method to modify the instances of String and Person class Person{ int a = 8; public int getA() { return a; } public void setA(int a) { this.a =…
8
votes
4 answers

what is the overhead of passing a reference?

how expensive is it to access a member variable when the getter in question return a reference? for example, if you have a class that needs to use such an accessor fairly often, how much more efficient would it be to store said reference in the…
Dollarslice
  • 9,917
  • 22
  • 59
  • 87
8
votes
4 answers

Do mainstream compilers convert passed-by-reference basic types into pass-by-copy?

Passing an object by reference is an easier, faster and safer way to pass an address to it. But for most compilers, it's all the same: references are really pointers. Now what about basic types like int? Passing an address to an int and using it…
Gabriel
  • 2,841
  • 4
  • 33
  • 43
8
votes
5 answers

Why does a temporary variable in Python change how this Pass-By-Sharing variable behaves?

first-time questioner here so do highlight my mistakes. I was grinding some Leetcode and came across a behavior (not related to the problem) in Python I couldn't quite figure out nor google-out. It's especially difficult because I'm not sure if my…
8
votes
1 answer

Passing a variable by reference into a PHP extension

I'm writing a PHP extension that takes a reference to a value and alters it. Example PHP: $someVal = "input value"; TestPassRef($someVal); // value now changed What's the right approach?
John Carter
  • 53,924
  • 26
  • 111
  • 144
8
votes
4 answers

PDO pass by reference notice?

This: $stmt = $dbh->prepare("SELECT thing FROM table WHERE color = :color"); $stmt->bindParam(':color', $someClass->getColor()); $stmt->execute(); yields this: Runtime notice Only variables should be passed by reference though it still…
Drew
  • 6,208
  • 10
  • 45
  • 68
8
votes
1 answer

std::for_each, calling member function with reference parameter

I have a container of pointers which I want to iterate over, calling a member function which has a parameter that is a reference. How do I do this with STL? My current solution is to use boost::bind, and boost::ref for the parameter. // Given: //…
Zack The Human
  • 8,373
  • 7
  • 39
  • 60
8
votes
4 answers

C++ dependency injection - by reference or by boost::shared_ptr?

In cases where constructor dependency injection is required, what are the considerations for using injection by reference vs. using boost::shared_ptr? Is there another common way of doing it? How does it compare to the two methods above?
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359
8
votes
1 answer

When implementing an interface that has a method with 'in' parameter by TypeBuilder.CreateType, TypeLoadException is thrown

Using TypeBuilder, I'm building a class that implements an interface that contains a method. After implementing that method with ILGenerator, then I call TypeBuilder.CreateType() and everything goes well in the normal case. But if the method…
Codeption
  • 141
  • 8
8
votes
6 answers

Properties - by value or reference?

I've got the following public property which exposes an Arraylist: public ArrayList SpillageRiskDescriptions { get { return _SpillageRiskDescriptions; } set { …
m.edmondson
  • 30,382
  • 27
  • 123
  • 206