pass-by-value

Why is a volatile local variable optimised differently from a volatile argument, and why does the optimiser generate a no-op loop from the latter?

妖精的绣舞 提交于 2019-11-28 00:17:35
问题 Background This was inspired by this question/answer and ensuing discussion in the comments: Is the definition of “volatile” this volatile, or is GCC having some standard compliancy problems?. Based on others' and my interpretation of what should happening, as discussed in comments, I've submitted it to GCC Bugzilla: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71793 Other relevant responses are still welcome. Also, that thread has since given rise to this question: Does accessing a declared

Is it a good practice to change arguments in Java

拈花ヽ惹草 提交于 2019-11-27 23:53:11
问题 Suppose I am writing a method foo(int i) in Java. Since i is passed by value it is safe to change it in foo . For example void foo(int i) { i = i + 1; // change i ... } Is it considered good or bad practice to change arguments of methods in Java? 回答1: It's considered bad practice in general, though some people overlook it as you can see in the other answers. For parameters like primitives that are directly passed in by value, there is no advantage in overriding the original variable. In this

Java - Object state does not change after method call [duplicate]

让人想犯罪 __ 提交于 2019-11-27 23:45:10
This question already has an answer here: Is Java “pass-by-reference” or “pass-by-value”? 82 answers Beginner java question, but I cannot understand how call-by-Value ( or Reference ) is working in the example below - How come the String value is not modified after it exits the method while my custom String Object is. ? Same with other classes like Date.. public class StringMadness { public static void main(String[] args) { String s = "Native String"; CustomStringObject cs = new CustomStringObject(); System.out.println("Custom String Before: " + cs.str); hello(cs); System.out.println("Custom

why copy constructor is call when we pass an object as an argument by value to a method

笑着哭i 提交于 2019-11-27 23:42:50
i am new to C++ programming, when i am doing some C++ programs i have got a doubt that is why copy constructor is called when i pass an object as argument by value to a function. please see my below code in that i am passing a object of class as an argument by value to a function display() but it calling copy constructor and then control is hitting the display() function but i am understanding why it so please help. #include "stdafx.h" #include <iostream> using namespace std; class ClassA { private: int a, b; public: ClassA() { a = 10, b = 20; } ClassA(ClassA &obj) { cout << "copy constructor

Performance cost of passing by value vs. by reference or by pointer?

孤人 提交于 2019-11-27 21:56:47
Let's consider an object foo (which may be an int , a double , a custom struct , a class , whatever). My understanding is that passing foo by reference to a function (or just passing a pointer to foo ) leads to higher performance since we avoid making a local copy (which could be expensive if foo is large). However, from the answer here it seems that pointers on a 64-bit system can be expected in practice to have a size of 8 bytes, regardless of what's being pointed. On my system, a float is 4 bytes. Does that mean that if foo is of type float , then it is more efficient to just pass foo by

How are arrays passed?

人盡茶涼 提交于 2019-11-27 20:55:37
问题 Are arrays passed by default by ref or value? Thanks. 回答1: They are passed as pointers. This means that all information about the array size is lost. You would be much better advised to use std::vectors, which can be passed by value or by reference, as you choose, and which therefore retain all their information. Here's an example of passing an array to a function. Note we have to specify the number of elements specifically, as sizeof(p) would give the size of the pointer. int add( int * p,

Does C++ pass objects by value or reference?

北城余情 提交于 2019-11-27 18:19:13
A simple question for which I couldn't find the answer here. What I understand is that while passing an argument to a function during call, e.g. void myFunction(type myVariable) { } void main() { myFunction(myVariable); } For simple datatypes like int , float , etc. the function is called by value. But if myVariable is an array, only the starting address is passed (even though our function is a call by value function). If myVariable is an object, also only the address of the object is passed rather than creating a copy and passing it. So back to the question. Does C++ pass a object by

How to modify an array in function?

老子叫甜甜 提交于 2019-11-27 17:41:11
问题 MATLAB is a pass by value language. I have a recursive function that processes pixel's neighbors. It is very expensive to make the copy of the image (in my case two images) each time the function is called. I used global variables to solve the problem. Is there any other way to make a recursive function modify an array? 回答1: You have three options here, but maybe you don't need any of them, since Matlab used 'copy-on-write', i.e. variables are only copied if you modify them. As @gnovice

python pandas dataframe, is it pass-by-value or pass-by-reference

你。 提交于 2019-11-27 17:26:24
If I pass a dataframe to a function and modify it inside the function, is it pass-by-value or pass-by-reference? I run the following code a = pd.DataFrame({'a':[1,2], 'b':[3,4]}) def letgo(df): df = df.drop('b',axis=1) letgo(a) the value of a does not change after the function call. Does it mean it is pass-by-value? I also tried the following xx = np.array([[1,2], [3,4]]) def letgo2(x): x[1,1] = 100 def letgo3(x): x = np.array([[3,3],[3,3]]) It turns out letgo2() does change xx and letgo3() does not. Why is it like this? The short answer is, Python always does pass-by-value, but every Python

Rule of thumb for when passing by value is faster than passing by const reference?

流过昼夜 提交于 2019-11-27 15:32:30
问题 Suppose I have a function that takes an argument of type T . It does not mutate it, so I have the choice of passing it by const reference const T& or by value T : void foo(T t){ ... } void foo(const T& t){ ... } Is there a rule of thumb of how big T should become before passing by const reference becomes cheaper than passing by value? E.g., suppose I know that sizeof(T) == 24 . Should I use const reference or value? I assume that the copy constructor of T is trivial. Otherwise, the answer to