pass-by-value

.click() callback references local variable from the calling method instead of copying by value

The following jQuery Javascript code is included on an otherwise empty page. var element; $(function() { for (var i = 0; i < 10; i++) { element = $('<div>' + i + '</div>'); element.click(function() { alert(i); }); $('body').append(element); } }); The desired behavior is that this code should generate 10 div e...

PHP performance difference between passing variables method

Hi everyone, Will there be any measurable performance difference when passing data as values instead of as reference in PHP? It seems like few people are aware of that variables can be passed as values instead of references. Is this common sense or not? ...

Using references in PHP.

I ask this question because i learned that in programming and designing, you must have a good reason for decisions. I am php learner and i am at a crossroad here, i am using simple incrementation to try to get what im asking across. I am certainly not here to start a debate about the pros/cons of referencing, but when it comes to php, ...

Possible to prevent Java's pass by value in methods?

Java is a 'pass by value' language, meaning that sending in a variable into a method, pointing the variable to a new object, does not effect the outer variable. public void one() { String s = "one"; two(s); System.out.println(s); } public void two( String s ) { s = "two"; } Would write "one". Is there a way to prevent this? Or wha...

Automatic Evaluation Strategy Selection in C++

Consider the following function template: template<typename T> void Foo(T) { // ... } Pass-by-value semantics make sense if T happens to be an integral type, or at least a type that's cheap to copy. Using pass-by-[const]-reference semantics, on the other hand, makes more sense if T happens to be an expensive type to copy. Let's ass...

[c++] Problem with passing by reference

Can I overload a function which takes either a reference or variable name? For example when I try to do this: void function(double a); void function(double &a); I would like the caller of this function to be able to do: double a = 2.5; function(a); // should call function(double &a) function(2.3); // should call function(double a) ...

Good practice to edit objects "by reference"?

Hi. 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 editSuperstar(Superstar superstar){ .... superstar.setEdited(true); return superst...

C++ Pass by Reference Program

IBM explains C++ pass by reference in the example below (source included). If I changed void swapnum... to void swapnum(int i, int j), would it become pass by value? // pass by reference example // author - ibm #include <stdio.h> void swapnum(int &i, int &j) { int temp = i; i = j; j = temp; } int main(void) { int a = 10; ...

C Programming - Pass-by-Reference

In the C program below, I don't understand why buf[0] = 'A' after I call foo. Isn't foo doing pass-by-value? #include <stdio.h> #include <stdlib.h> void foo(char buf[]) { buf[0] = 'A'; } int main(int argc, char *argv[]) { char buf[10]; buf[0] = 'B'; printf("before foo | buf[0] = %c\n", buf[...

Pass by copy or reference?

I am a bit new to C# and was just wondering if there is a list of classes (commonly used) which are by default passed as copy. How can I identify them? I know the basic basic object types — int, uint, float, strings, ... — are passed by copy. ...

Array parameter passing in C#: why is it implicitly by reference?

Assume the following code, without any ref keyword, that obviously won't replace the variable passed, since it's passed as value. class ProgramInt { public static void Test(int i) // Pass by Value { i = 2; // Working on copy. } static void Main(string[] args) { int i = 1; ProgramInt.Test(i); ...

Return a value or modify reference?

I've seen both before, and as far as I know, it's pretty much subjective, but if given the option, which would you do and why? If the data were large, would there be any speed/memory benefit to one of them? function processData(&$data_to_process) { // Pass by reference. // do something to the data } // ... somewhere else $this->p...

C++ Pointer problem

I'm having a pointer problem that I can't seem to figure out. It seems like I've used pointers in this way a 1000 times so I'm not quite sure what is going on here. I have the following code: int iRetVal; CycleCountOrder* cycleOrder = NULL; CycleCountLineItem* cycleLine = NULL; iRetVal = m_CycleCount.GetCCOrderLine(pOneLocation.szO...

Passing object to method in java appears to be by reference (and Java is by val)

I thought when you passed objects to methods in Java, they were supposed to be by value. Here is my code: public class MyClass{ int mRows; int mCols; Tile mTiles[][]; //Custom class //Constructor public MyClass(Tile[][] tiles, int rows, int cols) { mRows = rows; mCols = cols; mTiles = n...

pass object by value to another thread

I am writing a little trial project and I need to pass and object of type QueuList by value to a thread pool thread. It is a Boost threadpool and I am using Bind to pass the args to the thread. For some reason I cannot seem to pass my item to the threadpool thread by value... Can anybody help what I'm doing wrong? void ConsumerSchedu...

Template parameters in C++

Suppose I have arbitrary template method, which could receive parameters by value and by const reference (obviously, for trivial types and for objects accordingly). How is this situation handled when writing template function prototypes? I could make something like: template <typename T> void Foo(T value) { // Do something. } temp...

Problems with malloc on OS X

Hi there, I have written a some C code running on OS X 10.6, which happens to be slow so I am using valgrind to check for memory leaks etc. One of the things I have noticed whilst doing this: If I allocate memory to a 2D array like this: double** matrix = NULL; allocate2D(matrix, 2, 2); void allocate2D(double** matrix, int nrows, int...

Is there simple PHP code to distinguish "Passing the object as reference" vs "Passing the object reference as value"?

This is related to question: http://stackoverflow.com/questions/3957584/how-does-operator-work-in-php-function/3957669#3957669 Is there simple code to show the difference between passing the object as reference vs passing the object's reference as value? ...

Java recursion: pass by reference

I realize this is a hotly debated, controversial topic for Java programmers, but I believe my problem is somewhat unique. My algorithm REQUIRES pass by reference. I am doing a clockwise/counterclockwise pre-order traversal of a general tree (i.e. n-children) to assign virtual (x,y) coordinates. This simply means I count (and tag) the nod...