Menu
  • HOME
  • TAGS

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

In C++11, you should add rvalue reference: A(A&&); void operator=(A&& a ); ...

Why dynamically allocated memory of a function parameter is lost when exiting the function? [duplicate]

c,memory,pass-by-value

You lack a level of indirection, you need char**. Excuse the bad formatting, I write from my phone. Char* array, array is bound to a memory slot (that will contain a value that points to another memory slot that would be interpreted as a char). So you copy that value...

Does C# pass a List to a method by reference or as a copy? [duplicate]

c#,pass-by-reference,pass-by-value

It's passed by reference. List<T> is a class, and all classes are passed by reference.

Does D pass value by copy?

d,pass-by-value,pass-by-pointer

Classes in D use reference semantics so b points to the same object as a. structs, on the other hand, use value semantics so... auto a = mystruct(); auto b = a; ...would refer to distinct objects....

Should I pass a large array by reference or value?

performance,pass-by-reference,rust,pass-by-value

I can't answer your question directly because, as with so many other things, it depends on where the code is, what the code around it is doing, what the access patterns are, blah, blah, blah. If you're concerned about performance, you have to profile. That said... "Large" values passed by...

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

python,object,multidimensional-array,pass-by-reference,pass-by-value

Actually, you use the same lists in adjStates.append(createState(self.curMatrix, xxxPos)) def createState(stateMatrix, position): newMatrix = list() newMatrix = [stateMatrix[row] for row in range(len(stateMatrix))] newMatrix[position.row][position.column] = 'X' return State(newMatrix, position) I suppose you need copies of original lists, so change the above code to: def createState(stateMatrix, position): # it is superfluous code,...

Are const arguments of function are passed by reference automatically?

c++,const,pass-by-reference,pass-by-value

At the language level, if you pass it by value then, of course, it is passed by value, not by reference. But how the passing is implemented physically under the hood is an implementation detail, which is not that dependent on the argument being const as it might seem at...

param value with different passing mechanism

c++,pass-by-reference,pass-by-value

for the "call by reference" for f(y,z,y), first let us rewrite the code of the f function int f(float & i, float & j, float & k) { float t=i; i=j; j=k; k=t; } (I renamed the local variable x to t to avoid confusions) Calling f(y,z,y) amounts to the...

VB.Net why is my marshaled structure updated when passed ByVal but not when passed ByRef

.net,structure,marshalling,pass-by-reference,pass-by-value

The midiOutPrepareHeader method's second parameter is a "Pointer to a MIDIHDR structure that identifies the buffer to be prepared". Your IntPtr is that pointer. By declaring the parameter ByRef you were passing a pointer to the pointer instead of passing the pointer itself.

pointer to a struct becomes NULL at every function call

c,pointers,pass-by-value

With a great help of users (which shared some very useful links), I have managed solving it. Solution: to modify caller memory, pass a pointer to that memory. After a bit updating, I leave here correctly working code: #include <stdio.h> #include <stdlib.h> struct DoubleList { int id; struct DoubleList *next;...

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

c,pointers,malloc,pass-by-reference,pass-by-value

You need this: void alloco(int **ppa) { int i; printf("inside alloco %p\n",ppa); *ppa = malloc(20 * sizeof(int)); (*ppa)[15] = 9; // rather pointless, in the loop below (*ppa)[15] will be // overwritten anyway printf("size of a %d \n", sizeof(*ppa)); // rather pointless, not sure // what you want to print...

Why isn't Java acting like “Pass-By-Value” here? [duplicate]

java,parameter-passing,pass-by-value

It is behaving as pass-by-value. Your confusion, I think, stems from what that means for non-primitive parameters (and an array is an object, not a primitive). For object parameters, what's passed by value is always the reference to the object. In your recursive method, you are modifying the elements of...

C# Object property modified in the called function affect caller value

c#,class,pass-by-reference,pass-by-value

You can change the object passed to the method, but you can't change the reference itself without the ref keyword public class MyClass { public String TestProperty { get; set; } } public class TestClass { public TestClass() { var myClass = new MyClass(); myClass.TestProperty = "First Modification"; MyFunction(myClass); Console.WriteLine(myClass.TestProperty);...

iOS pass the value instead of reference

ios,nsmutablearray,nsmutabledictionary,pass-by-value

Copy the array and pass in the copy. NSArray *friendList = [friends_dict valueForKey:selectedFriendId]; NSMutableArray *friendsArray = [friendList mutableCopy]; ...

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

javascript,pass-by-value

You can't pass an object by reference. But you can clone an object before you pass it. The simplest is to use Object.create(): Bot.prototype.fire = function () { this.bombs.push(new Bomb(Object.create(this.position))); }; ...

Pass by Reference issue-Java

java,pass-by-reference,pass-by-value

The infinite loop you are getting is because you are calling smaller() from inside compute() and compute() from inside smaller(). If this is done intentionally then also add a terminating condition , which would prevent it from looping infinitely.

realloc() fails when passing pointer to char *. Why? (Because it was by-value, not reference!)

c,string,realloc,pass-by-value

The error is that C passes arguments by value, meaning they are copied. So when you assign to currBuffer in the function you only assign (and modify) the local copy. When the function returns, the old userInput is still unchanged. To fix this you have to pass the pointer by...

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

java,pass-by-reference,pass-by-value

because the return instruction exits the code hence the methods done its job it doenst need to iterate again once it reaches the return instruction, i however would have done it if the return instruction wasnt reached.

Passing object from Main() to function changes the object , even though C# is by value?

c#,pass-by-reference,pass-by-value

Person is a reference type, so when you pass it to changeName as value what you are really passing is a pointer (address) to that Person object in memory. Since that pointer is passed by value you can't re-assign it to a different copy of Person, but you can call...

Value of Passed-By-Value Struct Does Not Change

c,pointers,struct,linked-list,pass-by-value

First off, you're assigning NULL to your input. This: if (tester = NULL) should be if (tester == NULL) Secondly, in that same branch, you assign a new value to tester. However, everything in C is passed by value (copy), so your function receives a copy of a pointer. Therefore,...

Understanding Java Arrays

java,arrays,pass-by-reference,pass-by-value

I'll answer your edit question. public static void main(String[] args) { StringHolder[] holders = new StringHolder[]{new StringHolder("string 1")}; StringHolder tmp = holders[0]; holders[0].setValue("string 2"); System.out.println(tmp); // string 2 System.out.println(holders[0]); // string 2 } Because both are holding a reference to the same object. public static void main(String[] args) { StringHolder[]...

References and Casting

java,reference,pass-by-reference,pass-by-value

If there are multiple variables referencing the same object, changes in the object can be done using any of the references, but changes in the references only affect that reference. In your example, a and a1 reference the same array. If you modify the referenced object, the array of A,...

Swap entire arrays in Javascript

javascript,arrays,pass-by-reference,pass-by-value

They are passed by reference, but they are also assigned by reference. When you write x = y you aren't modifying either of the arrays, you're just making your local variable x refer to the same array as y. If you want to swap the array contents, you have to...

PHP array passed by reference?

php,arrays,typo3,pass-by-reference,pass-by-value

Arrays are passed by value indeed (or as some have pointed by reference if they are not modified in the function or method, see the Rohit comment above). I thought the arrays were different but were not. It was just fatigue. Debugging in late hours is not productive. Anyway, I...

std::forward with pass-by-value argument

c++,c++11,pass-by-value,perfect-forwarding

If Model<T> where T is an lvalue reference type (e.g. X&) is legal (according to Model's documentation), then forward is the correct tool to use here. Otherwise (if T should always be an object type), move is the correct tool. That being said, the clone member function makes it look...

Operator overloading by value results in use of moved value

copy,operator-overloading,rust,pass-by-value,ownership

If you don't want to copy then, as far as my newbie understanding goes, you need to implement Add on references to Point. This would be supported by the RFC: Fortunately, there is no loss in expressiveness, since you can always implement the trait on reference types. However, for types...

In C# should I pass a parameter by value and return the same variable, or pass by reference?

c#,methods,pass-by-reference,argument-passing,pass-by-value

List like all reference types, is passed as a reference to the object, and not a copy of it. Note that this is very different from saying it is passed by reference, as that would imply assignment of the parameter propagates to the caller, which it does not It does...

Why are objects automatically passed by reference?

c#,clone,pass-by-reference,pass-by-value,shallow-copy

Why are objects automatically passed by reference? They're not. Is there any particular benefit from forcing the cloning process for them instead of treating objects more like int, double, boolean, etc. in these cases? There's no "cloning process" for reference types, only for value types. I think you're confusing...

C++ Pass by Reference and Value and Const Issues

c++,const,pass-by-reference,pass-by-value

You're right that if you're passing by value, there's no need to make it a const value. To answer the first part of your question, if you return a CView and pass it into the process functions, it will not be copied again because the ProcessAdd, ProcessReverse, and ProcessMul functions'...

C++ memory management when passing shared_ptr to lambda

c++,lambda,shared-ptr,pass-by-value,reference-counting

Your understanding is correct. Also, if the lambda object is copied (as part of wrapping it in a std::function<void()> perhaps), then that will also increment the reference count (and decrement it when the copy is destroyed)....

The better technique in this refactoring case?

vb.net,refactoring,pass-by-value

You can pass the variables by reference (not by value) to the function, it's not a bad practice and should work: Public Sub MyFunc(ByRef node As MyNode, ByRef typ As String) node = ... typ = ... End Sub Or you can return some complex data holder: Public Class MyParams...

Copy of object by passing as method parameter

java,oop,pass-by-reference,pass-by-value,objectinstantiation

If you store objects in variables or parameters you always store the reference. The object is never copied. Only primitive types, such as int or boolean are copied. If you want to create copies of objects you should look into object cloning using the java.lang.Cloneable interface and the Clone method...

Python multiprocessing: object passed by value?

python,threadpool,pass-by-reference,pass-by-value,python-multiprocessing

As André Laszlo said, the multiprocessing library needs to pickle all objects passed to multiprocessing.Pool methods in order to pass them to worker processes. The pickling process results in a distinct object being created in the worker process, so that changes made to the object in the worker process have...

argument type of assignment operator (reference or value?)

c++,operator-overloading,pass-by-reference,pass-by-value

You just need to take in the argument by reference-to-const: MyClass& operator= (const MyClass&); const references can bind to rvalues, so sum = A + B is valid....

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

You can use a closure around the addListener part: (function(text){ google.maps.event.addListener(marker, 'click', function () { ShowMarkerContentPopUp(this, text); }); })(text); This creates a new scope and takes the text variable as an argument. It means the text inside the closure is protected from being changed outside of the closure. The same...

Reference type confusing in Go language

go,pass-by-reference,pass-by-value,trie,pass-by-name

The problem is in method containsIndex. Golang range by default creates copy each element in slice and assigns copy of this value to st (in your example). Usually to preserve reference to element in slice you should use original slice and its index. In you case method containsIndex should look...

Passing a pointer to a function

c++,function,pointers,pass-by-value

Here, the root itself has been passed to insert() using pass-by-value. so, from insert(), the value of root cannot be changed. In other words, the cur is local to insert() function. Any changes made to cur itself won't impact the actual argument passed. If you want to change the value...

Scanf - varaible per reference VS parameter per reference

c,pointers,pass-by-reference,scanf,pass-by-value

There are two function calls happening in your code that need to be able to modify the data in the caller: The call of inputNums(...), and The call of scanf(...), which happens from inside inputNums(...) In order for the nested call of scanf to be able to modify data in...

Is Swift Pass By Value or Pass By Reference

swift,pass-by-reference,pass-by-value,pass-by-pointer

Types of Things in Swift The rule is: Class instances are reference types (i.e. your reference to a class instance is effectively a pointer) Functions are reference types Everything else is a value type; "everything else" simply means instances of structs and instances of enums, because that's all there is...

Why is Java pass by value only? [closed]

java,pass-by-value

O'Reilly's Java in a Nutshell by David Flanagan puts it best: "Java manipulates objects 'by reference,' but it passes object references to methods 'by value.'" This was a design decision by Java. When you pass objects around, you are still manipulating the same underlying object as they all reference the...

Setting Pointer References

c,table,hash,pass-by-reference,pass-by-value

Accept a hash_table ** instead of a hash_table *. Then if your caller has a hash_table *ht, they will call hash_rehash(&ht), allowing the function to modify ht.

How to understanding pointers in functions

c,function,pointers,pass-by-value

you never initialize local_number in the second program. It does not point anywhere, and will crash when accessed. Try int *local_number = &global_number; then the value should change To have change_number also initialize local_number, pass the address of local_number and change the pointed-to pointer: void change_number( int **number ) {...

Passing argument by value to onClick function

javascript,for-loop,onclick,pass-by-value

What you're doing is creating a global variable and overwriting it every time you add to the .innerHTML. Code in a string can't reference local variables, so what you want isn't possible. One option would be to concatenate the value into the string. place.innerHTML += "<div class='container-fluid' name='blockName' id='blockName" +...

Coding Style — Pass by Reference or Pass by Value?

c,styles,pass-by-reference,pass-by-value

Here is a general rule of thumb: pass by value if its typdef is a native type (char, short, int, long, long long, double or float) pass by reference if it is a union, struct or array Additional considerations for passing by reference: use const if it will not be...

Preferred ruby idiom for passing a counter by reference

ruby,parameter-passing,pass-by-reference,counter,pass-by-value

If there's two rules in Ruby it's everything is an object, and every variable acts like a reference to an object. This might seem a little odd, but it's actually very consistent. In other languages which mix primitives and objects, references and values, there's often more confusion. Instead of a...

C++ view types: pass by const& or by value?

c++,c++11,parameter-passing,pass-by-reference,pass-by-value

When in doubt, pass by value. Now, you should only rarely be in doubt. Often values are expensive to pass and give little benefit. Sometimes you actually want a reference to a possibly mutating value stored elsewhere. Often, in generic code, you don't know if copying is an expensive operation,...

Function argument: original variable is modified even if passed by value [duplicate]

python,python-3.x,parameter-passing,pass-by-reference,pass-by-value

You have to create a deepcopy: from copy import deepcopy dist = getDistances(deepcopy(qt), ex) ...

Rewind File, Create Dynamic Struct

c,arrays,dynamic,stream,pass-by-value

I think you have some warning from gcc to help you. Fix memory management with your calloc, and don't return a stack pointer typedef struct _data { char* name; long number; } _data; _data *load(FILE *stream, int size) { _data *BlackBox = calloc(size, sizeof(_data)); char tempName[3]; for (int i=0; i<size;...

Counter Intuitive Object Parameter Behaviour

java,parameters,pass-by-reference,pass-by-value

In Java, parameters are always passed by value, so test is actually a copy of test1. The mistake you're making is that copying an object variable does not copy the object; it copies the reference to the same object. Since you're mutating the fields on the same object instance, both...

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

java,pass-by-reference,pass-by-value

Arrays are reference types, so the iArr variable holds a reference to an array. In other words, when you call Arrays.sort(iArr); you're passing a reference (by value) to the sort method, which sorts the array that iArr refers to. From comments: What does passing a reference (by value) actually mean?...

Update Tree node using pass by value in Java

java,arraylist,pass-by-reference,pass-by-value

Passing in a reference to a method means the reference is a value. This value is made available to you by the name of the method parameter root, which lives on the stack. Assigning different references to the parameter root will be reflected only inside the method, but as soon...

Why do these blocks of code behave differently?

perl,pass-by-reference,subroutine,pass-by-value,pass-by-pointer

In the first example, you use the reference to get the array and then you modify that. There is only one array and you change it. In the second example, you use the reference to get the array, then you copy the content of the array into a second array,...

Qt:signal slot pass by const reference

c++,qt,pass-by-reference,signals-slots,pass-by-value

When you pass an argument by reference, a copy would be sent in a queued connection. Indeed the arguments are always copied when you have a queued connection. So here there would be no trouble regarding the life time of images since it will be copied instead of passed by...

Is the constructor not called for function parameters?

c++,constructor,destructor,pass-by-value

What's being called is the object's copy constructor, not its default constructor. Since you didn't define the copy constructor explicitly the compiler defined it implicitly (with no output of course). class B { public: B() { cout<<"Construct B"<<endl; } /// Add this B(const B&) { cout<<"Copy B"<<endl; } virtual ~B()...

how to pass a substring or single character within datagridview

c#,string,winforms,datagridview,pass-by-value

You can do with 'Split' function. this may give you a hint to proceed. string test = "animal?species"; string [] splittedtest = string.Split(new Char[] { '?', '\' }, StringSplitOptions.RemoveEmptyEntries); //string[] splittedtest = test.Split('?'); for(int i = 0;i<splittedtest.length;i++) { // do your stuffs like below, // DataGridView1.Rows[DataGridView.SelectedRows[0].Index].Cells[X].Text = splittedtest[i]; // where...

Java Array Equality vs Array Content Equality

java,arrays,double,pass-by-reference,pass-by-value

When you did, nonZeros = lastVal; You assigned the reference of lastVal to nonZeros (and yes, nonZeros became a pointer that simply pointed to lastVal). That's not a copy. You could use System.arraycopy((Object src, int srcPos, Object dest, int destPos, int length) like System.arraycopy(lastVal, 0, nonZeros, 0, nonZeros.length); ...

I don't understand how Java is pass by value [duplicate]

java,pass-by-reference,pass-by-value

So I thought Java was pass by value It is. ...but why does the following code...print the following? Because the value passed for reference types is the reference, not a copy of the object. "Pass by reference" refers to a completely different thing, passing a reference to the variable....

how does void methods behaves when passing object reference or primitive types as a parameter [duplicate]

java,pass-by-value

stringBuilderAppend: The method is not returning anything. You are passing an object and modify its state inside the methods body. As you keep a reference to the same object in the calling method, you see the changes made on that object. This is a fundamental concept of object orientation which...