Questions tagged [pass-by-value]

pass-by-value is a "one way passing" so that modifications of the passed value inside the receiving function (or other entity like process, etc) are not returned back.

pass-by-value is a one way passing so that modifications of the passed value inside the receiving function (or other entity like process, etc) are not returned back.

pass-by-value normally means cloning a variable and making a cloned copy available to the receiving function. Modifications of this copy are allowed, but they are not returned back to the caller; the copy is simply disposed after the call returns.

Passing by value is more secure if the function can also be called by malicious code (malicious code can retain the reference and observe it later even if it cannot modify it). Some are also convinced it results a cleaner code. However passing of the larger structures by value also requires more resources.

1173 questions
13
votes
2 answers

Why was destructor executed twice?

#include using namespace std; class Car { public: ~Car() { cout << "Car is destructed." << endl; } }; class Taxi :public Car { public: ~Taxi() {cout << "Taxi is destructed." << endl; } }; void test(Car c) {} int main() { …
13
votes
5 answers

pass by reference and value with pointers

I don't understand why passing a pointer to a function doesn't change the data being passed in. If the function proto looked like this: void func( int *p ); and func allocated memory to p, why can't it be used outside of the function? I thought…
TheFuzz
  • 2,607
  • 6
  • 29
  • 48
13
votes
1 answer

Why the second argument to pthread_join() is a **, a pointer to a pointer?

I am new to using pthread and also not that familiar with pointers to pointers. Could somebody perhaps explain why the second argument of pthread_join() is a void **. Why is it designed like this. int pthread_join(pthread_t thread, void…
artic sol
  • 349
  • 6
  • 18
13
votes
2 answers

java Boolean value not changing in called method

I have a scenario where I want to set a Boolean object and then use its booleanValue() in a constructor later on in the method. However, the scope in which the object is set is different. It is set in a method called by the method in which the…
user1849060
  • 621
  • 3
  • 10
  • 20
13
votes
9 answers

Pass-by-value (StringBuilder vs String)

I do not understand why System.out.println(name) outputs Sam without being affected by the method's concat function, while System.out.println(names) outputs Sam4 as a result of the method's append method. Why is StringBuilder affected and not…
Helenesh
  • 3,999
  • 2
  • 21
  • 36
13
votes
2 answers

Is there a rational explanation for this PHP call-by-value behavior? Or PHP bug?

PHP 5.5.12. Consider this: aq [1] => bq [2] => cq ) Now consider:
Nairebis
  • 283
  • 1
  • 8
12
votes
2 answers

Why does std::find_if(first, last, p) not take predicate by reference?

I was looking at the various signatures for std::find_if on cppreference.com, and I noticed that the flavors that take a predicate function appear to accept it by value: template< class InputIt, class UnaryPredicate > InputIt find_if( InputIt first,…
aghast
  • 14,785
  • 3
  • 24
  • 56
12
votes
3 answers

Pass an array by value

Generally when we pass array by its name, it’s call by address. That means if we change any value of array outside main() it will be reflected in main(). So, what should I do to if I want to pass the array as an argument of a function and call it…
Fahim Al Mahmud Ashik
  • 1,083
  • 10
  • 26
12
votes
7 answers

Passing to a Reference Argument by Value

Consider this simple program: vector foo = {0, 42, 0, 42, 0, 42}; replace(begin(foo), end(foo), foo.front(), 13); for(const auto& i : foo) cout << i << '\t'; When I wrote it I expected to get: 13 42 13 42 13 42 But instead I got: 13…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
12
votes
5 answers

Good practice to edit objects "by reference"?

Let's say I've got a type called Superstar. Now I want to have a method that does some work and edits some properties of a Superstar object. Here are two ways of how I could implement this. Way 1 would be the following: private Superstar…
user321068
12
votes
5 answers

Updating pointers in a function

I am passing a pointer a function that updates it. However when the function returns the pointer it returns to the value it had prior to the function call. Here is my code: #include #include static void func(char *pSrc) { …
asicman
  • 133
  • 1
  • 1
  • 4
12
votes
5 answers

Parameter passing in Java

I know that Java is always pass-by-value, but I do not understand why this works: public static void swap(int[] arr, int i, int j) { int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } public static void main(String[] args) { int[]…
SorcerOK
  • 169
  • 9
12
votes
4 answers

Java Pass By Value and Pass By Reference

i have done the following sample with to check my knowledge import java.util.Map; public class HashMap { public static Map useDifferentMap(Map valueMap) { valueMap.put("lastName", "yyyy"); return…
Joe
  • 4,460
  • 19
  • 60
  • 106
11
votes
1 answer

Are arguments passed to methods by reference or value?

Can anyone expand upon, correct, or verify what I feel is happening when you pass arguments to a method in Ruby. Are any of these points wrong? Am I missing any pieces? Everything in Ruby is an object. Variables are references to objects (When…
slindsey3000
  • 4,053
  • 5
  • 36
  • 56
11
votes
4 answers

In C++11, when are a lambda expression's bound variables supposed to be captured-by-value?

I have a Visual Studio 2010 C++ program, the main function of which is: vector v(10); double start = 0.0; double increment = 10.0; auto f = [&start, increment]() { return start += increment; }; generate(v.begin(), v.end(), f); for(auto it =…
Evan Harper
  • 430
  • 3
  • 15