pass-by-value

Passing to a Reference Argument by Value

泄露秘密 提交于 2019-12-01 15:20:23
Consider this simple program: vector<int> 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 42 0 42 0 42 The problem of course is that replace takes in the last 2 parameters by reference. So if either of them happen to be in the range being operated on the results may be unexpected. I can solve this by adding a temporary variable: vector<int> foo = {0, 42, 0, 42, 0, 42}; const auto temp = foo.front(); replace(begin(foo), end(foo), temp, 13); for

Is RVO (Return Value Optimization) on unnamed objects a universally guaranteed behavior?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 06:07:10
This question is in different aspect (also limited to gcc). My question is meant only for unnamed objects . Return Value Optimization is allowed to change the observable behavior of the resulting program. This seems to be mentioned in standard also. However, this "allowed to" term is confusing. Does it mean that RVO is guaranteed to happen on every compiler. Due to RVO below code changes it's observable behavior: #include<iostream> int global = 0; struct A { A(int *p) {} A(const A &obj) { ++ global; } }; A foo () { return A(0); } // <--- RVO happens int main () { A obj = foo(); std::cout<<

How does Python referencing work?

霸气de小男生 提交于 2019-12-01 05:39:53
I am confused with Python referencing. Consider the following example: My task : To edit each element in the list d = { 'm': [1,2,3] } m = d['m'] m = m[1:] # m changes its reference to the new sliced list, edits m but not d (I wanted to change d) Similarly: d = { 'm': [1,2,3] } m = d['m'] m = m[0] # As per python referencing, m should be pointing to d['m'] and should have edited d In python everything goes by reference, then when is a new object created? Do we always need copy and deepcopy from copy module to make object copies? Please clarify. In Python a variable is not a box that holds

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

Deadly 提交于 2019-12-01 03:33:39
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 **value_ptr); To return a value via a function's argument you need to pass in the address of the variable to receive the new value. As pthread_join() is designed to receive the pointer value being passed to pthread_exit() , which is a void* , pthread_join() expects the address of a void* which in fact is of type void** . Example: #include <stdlib.h> /* for

Copy elision for pass-by-value arguments

…衆ロ難τιáo~ 提交于 2019-12-01 02:26:15
Given struct Range{ Range(double from, double to) : from(from), to(to) {} double from; double to; }; struct Box{ Box(Range x, Range y) : x(x), y(y) {} Range x; Range y; }; suppose we run Box box(Range(0.0,1.0),Range(0.0,2.0)) . Could a modern compiler with optimizations enabled avoid copying Range objects altogether during this construction? (i.e. construct the Range objects inside box to begin with?) interjay There are actually two copies being performed on each Range object passed to the constructor. The first happens when copying the temporary Range object into the function parameter. This

C++: How do I decide if to pass params by ref or by value?

自闭症网瘾萝莉.ら 提交于 2019-12-01 01:53:56
With C++ how do i decide if i should pass an argument by value or by reference/pointer? (tell me the answer for both 32 and 64bits) Lets take A. Is 2 32bit values more less or equal work as a pointer to a 32bit value? B to me seems like i always should pass by value. C i think i should pass by value but someone told me (however i haven't seen proof) that processors don't handle values not their bitsize and so it is more work. So if i were passing them around would it be more work to pass by value thus byref is faster? Finally i threw in an enum. I think enums should always be by value Note:

Which is faster? Pass by reference vs pass by value C++

梦想的初衷 提交于 2019-12-01 01:48:14
I thought that pass by reference should be faster then pass by value because the computer isn't copying data, it just points to the address of data. But, consider the following C++ code: #include <iostream> #include <cassert> #include <cmath> using namespace std; // do not use pass by reference& with this function, it will become so slow unsigned long long computePascal(const unsigned int row, const unsigned int position) { if (position == 1 || position == row) return 1L; unsigned long long left = computePascal(row-1, position-1); unsigned long long right = computePascal(row-1, position);

Python and Java parameter passing [duplicate]

守給你的承諾、 提交于 2019-12-01 01:44:29
问题 This question already has answers here : How do I pass a variable by reference? (26 answers) Closed 4 years ago . I've seen in several places, including the Python documentation that Python uses pass by "assignment" semantics. Coming from a Java background, where the common mistake of saying "Java passes primitives by value, and objects by reference" is as a result of having objects references passed by value, I can't help but wonder if Python is really doing the same thing. To me, the

Is Java pass by value Or pass by reference Or both? [duplicate]

ε祈祈猫儿з 提交于 2019-12-01 00:31:15
This question already has an answer here: Is Java “pass-by-reference” or “pass-by-value”? 84 answers Consider following case. List<Integer> listOne = new ArrayList<>(); List<Integer> listTwo = new ArrayList<>(); listOne.add(1);I think this happens due to listOne.add(2); listOne.add(3); Collections.reverse(listOne); listTwo = listOne; //listTwo has same reference Collections.reverse(listOne); System.out.println(listOne); //out put [1, 2, 3] System.out.println(listTwo); // same out put Java is pass by value, where values (for non primitive types) happen to be references. I think this provide

Which is faster? Pass by reference vs pass by value C++

为君一笑 提交于 2019-11-30 21:02:14
问题 I thought that pass by reference should be faster then pass by value because the computer isn't copying data, it just points to the address of data. But, consider the following C++ code: #include <iostream> #include <cassert> #include <cmath> using namespace std; // do not use pass by reference& with this function, it will become so slow unsigned long long computePascal(const unsigned int row, const unsigned int position) { if (position == 1 || position == row) return 1L; unsigned long long