pass-by-value

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

不打扰是莪最后的温柔 提交于 2019-12-18 05:47:06
问题 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, InputIt last, UnaryPredicate p ); If I understand them correctly, lambdas with captured variables allocate storage for either references or copies of their data, and so presumably a "pass-by-value" would imply that the copies of captured data are

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

浪子不回头ぞ 提交于 2019-12-18 04:47:15
问题 I have a Visual Studio 2010 C++ program, the main function of which is: vector<double> 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 = v.cbegin(); it != v.cend(); ++it) { cout << *it << ", "; } cout << endl << "Changing vars to try again..." << endl; start = 15; increment = -1.5; generate(v.begin(), v.end(), f); for(auto it = v.cbegin(); it != v.cend(); ++it) { cout << *it << ", "

Is Objective-C pass-by-value or pass-by-reference?

混江龙づ霸主 提交于 2019-12-18 00:29:26
问题 Since we always use pointers to define variables, I was wondering if Objective-C is "pass by value", since like Java, the actual value would be passed by using its reference. However, since it seems to be built up on top of C, would it have all the functionality of C? 回答1: C does not support pass-by-reference and Objective-C, being a strict superset of C doesn't either. In C (and Objective-C) you can simulate pass-by-reference by passing a pointer, but it's important to remember that you're

C++: Why pass-by-value is generally more efficient than pass-by-reference for built-in (i.e., C-like) types

北城余情 提交于 2019-12-17 09:39:58
问题 just as what indicated in the title 回答1: A compiler vendor would typically implement a reference as a pointer. Pointers tend to be the same size as or larger than many of the built-in types. For these built-in types the same amount of data would be passed whether you passed by value or by reference. In the function, in order to get the actual data, you would however need to dereference this internal pointer. This can add an instruction to the generated code, and you will also have two memory

Java is NEVER pass-by-reference, right?…right? [duplicate]

不想你离开。 提交于 2019-12-17 09:09:37
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Is Java “pass-by-reference”? I found an unusual Java method today: private void addShortenedName(ArrayList<String> voiceSetList, String vsName) { if (null == vsName) vsName = ""; else vsName = vsName.trim(); String shortenedVoiceSetName = vsName.substring(0, Math.min(8, vsName.length())); //SCR10638 - Prevent export of empty rows. if (shortenedVoiceSetName.length() > 0) { if (!voiceSetList.contains("#" +

Java is NEVER pass-by-reference, right?…right? [duplicate]

扶醉桌前 提交于 2019-12-17 09:09:14
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Is Java “pass-by-reference”? I found an unusual Java method today: private void addShortenedName(ArrayList<String> voiceSetList, String vsName) { if (null == vsName) vsName = ""; else vsName = vsName.trim(); String shortenedVoiceSetName = vsName.substring(0, Math.min(8, vsName.length())); //SCR10638 - Prevent export of empty rows. if (shortenedVoiceSetName.length() > 0) { if (!voiceSetList.contains("#" +

Is Java really passing objects by value? [duplicate]

耗尽温柔 提交于 2019-12-17 07:10:19
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Is Java pass by reference? public class myClass{ public static void main(String[] args){ myObject obj = new myObject("myName"); changeName(obj); System.out.print(obj.getName()); // This prints "anotherName" } public static void changeName(myObject obj){ obj.setName("anotherName"); } } I know that Java pass by value, but why does it pass obj by reference in previous example and change it? 回答1: Java always passes

Java, pass-by-value, reference variables

喜你入骨 提交于 2019-12-17 04:36:10
问题 I have a problem with understanding the "pass-by-value" action of Java in the following example: public class Numbers { static int[] s_ccc = {7}; static int[] t_ccc = {7}; public static void calculate(int[] b, int[] c) { System.out.println("s_ccc[0] = " + s_ccc[0]); // 7 System.out.println("t_ccc[0] = " + t_ccc[0]); // 7 b[0] = b[0] + 9; System.out.println("\nb[0] = " + b[0]); // 16 c = b; System.out.println("c[0] = " + c[0] + "\n"); // 16 } public static void main(String[] args) { calculate

Is passing pointer argument, pass by value in C++?

孤人 提交于 2019-12-17 02:21:16
问题 Is passing pointer argument, pass by value in C++? Since i see that any change to the pointer as such is not reflected outside the method. The changes i do by dereferencing the pointer is reflected though. In that case, is it acceptable/standard procedure to use pointer to pointer as argument to a function to modify the pointer value as such within a function? 回答1: Yes to both. Pointers are passed by value as anything else. That means the contents of the pointer variable (the address of the

Dynamic memory access only works inside function

∥☆過路亽.° 提交于 2019-12-16 19:05:26
问题 This question is meant to be used as a canonical duplicate for this FAQ: I am allocating data dynamically inside a function and everything works well, but only inside the function where the allocation takes place. When I attempt to use the same data outside the function, I get crashes or other unexpected program behavior. Here is a MCVE: #include <stdlib.h> #include <stdio.h> void create_array (int* data, int size) { data = malloc(sizeof(*data) * size); for(int i=0; i<size; i++) { data[i] = i