FAQ Database Discussion Community


Swap entire arrays in Javascript

javascript,arrays,pass-by-reference,pass-by-value
When I try to make a function to swap 2 arrays, the original arrays are left unaltered. function swap(x, y) { var temp = x; x = y; y = temp; } u=[1, 0]; v=[0, 1]; swap(u, v); console.log(u); console.log(v); This results in u as [1, 0] and v as...

PHP array passed by reference?

php,arrays,typo3,pass-by-reference,pass-by-value
I've been looking for an answer to this issue I have with an array but so far no answer here nor the web. We have a TYPO3 website that has indexed search configured and installed. We crawl records (~60000) using the crawler extension. Everything was configured and running fine but...

Javascript, pass-by-value an object to a constructor

javascript,pass-by-value
I'm developing a game using Node.js ; I have a Bot class containing a position attribute and a fire method. This method acts as follow: Bot.prototype.fire = function () { this.bombs.push(new Bomb(this.position)); }; Obviously, the bomb's position has to differ from the bot's position, so I want this.position to be...

Passing a pointer to a function

c++,function,pointers,pass-by-value
I was trying to implement BST using C++ , so i tried this: #include <iostream> #include <stdlib.h> struct node { int value; node* left; node* right; }; void insert(node *cur , int val) { if(!cur) { cur = new node; cur->value = val; cur->left = NULL; cur->right = NULL; return;...

References and Casting

java,reference,pass-by-reference,pass-by-value
I'm struggling to understand what I consider a fundamental question in Java. Below the answer is given that line 3 leans to a class cast exception. I can see that originally a1 pointed to an array of object A. But on line 1 doesn't a get set to point to...

Why do these blocks of code behave differently?

perl,pass-by-reference,subroutine,pass-by-value,pass-by-pointer
I am new to Perl, and I cannot figure this out. I have two seemingly identical sets of code, but one subroutine updates the value while another does not. In the first set of code, my understanding is that the reference to an array is passed, then the value at...

What operators do I have to overload to see all operations when passing an object to a function?

c++,operator-overloading,pass-by-reference,pass-by-value,pass-by-pointer
I would like to write a piece of code that shows all copy/assignment/delete etc. operations that are done on the object when passing it to a function. I wrote this: #include <iostream> class A { public: A(){std::cout<<"A()"<<std::endl;} void operator=(const A& a ){std::cout<<"A=A"<<std::endl;} A(const A& a){std::cout<<"A(A)"<<std::endl;} ~A(){std::cout<<"~A"<<std::endl;} }; void pv(A a){std::cout<<"pv(A...

Pass by Reference issue-Java

java,pass-by-reference,pass-by-value
During the method call compute(ee, temp1, s1, k) from method smaller() the argument value which am passing is not same as the one which am receiving, can someone help me with this issue? *This is my problem statement-Get the least number after deleting k digits from the input number. For...

Is the constructor not called for function parameters?

c++,constructor,destructor,pass-by-value
So, this might be kinda of an embarrassing question. I was re-enforcing my C++ basics and I ran into this curious case. The interesting part is that upon entering the function, the constructor is not called for the object lb but upon leaving the function the destructor is called for...

Copied old list reference being changed by new list object modification in python

python,object,multidimensional-array,pass-by-reference,pass-by-value
I'm working on this problem: http://codeforces.com/contest/540/problem/C It seems to be a standard breadth first search on a coordinate plane, so essentially, you can move horizontally or vertically, updating the new position's cell with the character 'X' each time you move In order to solve it, I have to create a...

Should I pass a large array by reference or value?

performance,pass-by-reference,rust,pass-by-value
I am building a Game Boy emulator in Rust. I created a readByteFromMemory() function which takes as a parameter an 8KB array which represents the Game Boy's internal memory which was 8KB. I am thinking of two options here: fn readByteFromMemory(internalRAM: [u8;0x2000]) -> u8 or fn readByteFromMemory(internalRAM: &[u8;0x2000]) -> u8...

Can we use pass by reference in java? If No How does java.util.Arrays.sort works?

java,pass-by-reference,pass-by-value
I used to think Java supports both pass by value and passby reference but i came accross many discussions like Java is always pass by value, with no exceptions, ever . Java is always pass-by-value Java always passes arguments by value NOT by reference. Java passes references by value.So you...

The better technique in this refactoring case?

vb.net,refactoring,pass-by-value
I need to do some refactoring (actually it's A LOT, but this small step will be very helpful for the whole process). So, let's say I've got this code snippet: If xmlDoc.SelectSingleNode("/dang") IsNot Nothing Then universalNode = xmlDoc.SelectSingleNode("/dang") Type = "dang" ElseIf xmlDoc.SelectSingleNode("/nang") IsNot Nothing Then universalNode = xmlDoc.SelectSingleNode("/nang") Type...

Qt:signal slot pass by const reference

c++,qt,pass-by-reference,signals-slots,pass-by-value
Many qt examples did the following things: signal: passByConstRef(const QImage &) slot: receivedByConstRef( const QImage &) They are connected through queued connection in different threads. In thread 1: {QImage image; image = fromData(...); emit passByConstRef(image); } In thread 2: receivedByConstRef(const QImage& image){ ... pixmap = fromImage(image); ... } Since the...

Understanding Java Arrays

java,arrays,pass-by-reference,pass-by-value
Please look at the following code : public static void main(String[] args) { Random random = new Random(); int[] array = new int[10]; Arrays.setAll(array, operand -> random.nextInt(10)); System.out.println(Arrays.toString(array)); swap(array, 0, 9); System.out.println(Arrays.toString(array)); } static void swap(int[] array, int i, int j) { int temp = array[i]; // pass by value...

std::forward with pass-by-value argument

c++,c++11,pass-by-value,perfect-forwarding
I was reading about encapsulated polymorphism and I came across a piece of code like that: template <typename T> struct Model<T> : Concept { Model<T>(T impl) : mImpl(std::forward<T>(impl)) { } virtual Concept* clone() const override { return new Model<T>(mImpl) } virtual void operator (const LogMessage::Meta& meta, const std::string& message) override...

param value with different passing mechanism

c++,pass-by-reference,pass-by-value
int f(float i, float j, float k) { float x=i; i=j; j=k; k=x;} main(){ float x=5; y=10 z=25; if(...){ f(x,y,z); } else{ f(y,z,y); /* what's x,y,z after this line using call-by-reference and call-by-name mechanism*/ } } I think I have a pretty good understand about these three passing mechanism. After...

Rewind File, Create Dynamic Struct

c,arrays,dynamic,stream,pass-by-value
this function will rewind file, create the dynamic array (of size), and read in the data, populating the _data struct dynamic array. Note that stream is passed by value this time. The function then returns the populated array of struct struct _data { char* name; long number; }; struct _data...

Why doesn't this return a new value over and over? java

java,pass-by-reference,pass-by-value
public static int someFunc(int a, int b){ while(a <=b){ a+= 1; return a; } return b; } so i was expecting it to return the new value over and over but it didnt, once i executed the code and saw for myself that, i realised it had something to do...

C++ memory management when passing shared_ptr to lambda

c++,lambda,shared-ptr,pass-by-value,reference-counting
Consider the following C++ code: void f(std::function<void()> func) { func(); } void g(std::shared_ptr<MyObject> myObjPtr) { myObjPtr->someMethod(); } void h(std::shared_ptr<MyObject> myObjPtr) { f([=](){ g(myObjPtr); }); } Are there any memory leaks? My understanding is myObjPtr is copied into the lamba and has its reference count incremented. Then it's copied into g()...

In JavaScript, when looping through a FOR loop, how do I pass the value of the item in an array to an anonymous function?

javascript,google-maps,anonymous-function,pass-by-value
In JavaScript, I need to loop through a for loop to get the value of an item in an array, and pass this to an anonymous function. A simplified example of this is below: var aFunctions = []; var aStrings = ["a","b","c"]; for (var i = 0; i < aStrings.length...

Changing the pointer inside a function does not reflect outside the function [duplicate]

c,pointers,malloc,pass-by-reference,pass-by-value
This question already has an answer here: How can I allocate memory and return it (via a pointer-parameter) to the calling function? 11 answers void alloco(int *ppa) { int i; printf("inside alloco %d\n",ppa); ppa = (int *)malloc(20); ppa[15] = 9; printf("size of a %d \n", sizeof(ppa)); for(i=0;i<20;i++) printf("a[%d] =...