Questions tagged [pass-by-reference]

Pass by reference is an argument marshalling strategy whereby a variable's location in memory is passed to a function, rather than a copy of the variable's value, although the function appears in the source code to receive the variable itself rather than a pointer to it.

Filter by
Sorted by
Tagged with
0 votes
1 answer
59 views

Pass by reference to base class constructor from derived class constructor

I would like to include a reference of a struct, Coord, within a class, Base, from the initializer list of derived class, Derived. When I pass the instance of Coord to the Derived class constructor, a ...
BooleanSmith's user avatar
1 vote
3 answers
61 views

itk c++ function parameter passing

I am trying the simple act of writing a function (encapsulation) that calculates an histogram from the input via the itk ImageToHistogramFilter. While this works fine I do not seem to be able to get ...
Ichwerdennauchsonst's user avatar
-2 votes
0 answers
35 views

How the List<List<Integer>> ans contains only empty subsets? [duplicate]

class Solution { public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> ans = new ArrayList<>(); generateSubsets(0, ans,new ...
Sarthak Sarangi's user avatar
0 votes
0 answers
28 views

Rust refs vs Cpp refs. Clarification around references, assignments, and copying in Cpp vs Rust [duplicate]

I'm coming back to cpp after 15+ years and suffering confusion regarding the many ref semantics. I haven't worked with c14 (onwards), and am trying to reason about things. I've been doing Rust ...
Avba's user avatar
  • 14.9k
0 votes
1 answer
61 views

How to use mutable references as arguments for generic functions in rust [duplicate]

I am attempting to practice using the stratergy pattern and thought to sort a vector using a function selected at runtime, ideally being able to change the data in place using a mutable reference. I ...
Joseph Stevens's user avatar
0 votes
1 answer
12 views

Node.js how to pass function argument by reference in practice

I have found the following suggestions on how to pass parameter by reference using built-in JavaScript functionality: [Javascript] Pass By Value And Pass By Reference In JavaScript TypeScript pass ...
MatrixRonny's user avatar
-1 votes
0 answers
95 views

Overload 'call by value ' and 'call by reference' function? [duplicate]

I would like to define a single name 'f' for a function with two overloads, one is 'call by value', the other 'call by reference' #include <iostream> using namespace std; int f (int a) { ...
Gabriel Costa's user avatar
0 votes
3 answers
120 views

Malloc is not allocating enough memory, despite being hardcoded to allocate exactly two characters

I have a function that, given a pointer to a char array string (Passed by reference), a char character (passed by value), and an unsigned short int length (passed by reference), is supposed to, if ...
Joseph Latvala's user avatar
0 votes
0 answers
54 views

Trouble Passing Objects to Functions in PowerShell

New user here. I don't do any programming/coding by trade and have limited educational experience, so most of what I know is from tinkering around. I'm working on a PowerShell script that builds a ...
Mikepedia's user avatar
0 votes
2 answers
124 views

Struct passed by value is modified in function in c

I have this code were i need to sort an array of struct without modifying it. I passed the array of struct by value and i used an int array with indexes to sort the struct. To sort the array i also ...
th3 wolf's user avatar
1 vote
0 answers
38 views

Singly Linked list partial traversal. Before node that references the dummy node is different than it at the end. How is this possible

Below I have some code to implement a singly linked list which implements the following Given the head of a singly linked list and two integers left and right where left <= right, reverse the ...
Luke's user avatar
  • 1,792
0 votes
1 answer
47 views

Passing a Map between Main class and and an extended Abstract class in Java

I have a Main class where I have a Map declared and passing this as a reference to a class that extends an Abstract class MainClass.java: public class MainClass{ private final Map<String, ...
Chethan Swaroop's user avatar
0 votes
2 answers
61 views

How do I call multiple python functions by reference to operate on a variable so that I can change the sequence of function calls easily?

I want to create a 'function pipeline', like a factory. Let's say I have the following functions: def func1(var): var = # do something with var return var def func2(var): var = # do ...
skeetastax's user avatar
  • 1,210
1 vote
1 answer
65 views

How are updates to var arrays handled in Swift memory (confused about CoW)

I have this function to do a quick select partition when sorting numbers in descending order. The function refers to arr defined outside its scope (partition is a nested func in another func). My ...
Parth's user avatar
  • 2,820
0 votes
0 answers
71 views

Why does a JavaScript object's reference works but not the value?

I'm constructing a linked list in JavaScript by following a tutorial and below is a sample code snippet. This does successfully create a linked list but I'm having a hard time understanding how the '...
Giridharan Selvaraju's user avatar
1 vote
1 answer
54 views

why this function not work in create BinTree

I want create this BinTree . in this function it can't work. typedef struct BinNode { Elemtype data; struct BinNode* left; struct BinNode* right; }BinNode,*BinTree; void createBinTree(...
Ly丶Cooki's user avatar
1 vote
1 answer
102 views

C++ pass iterator vs. pointer to function

I'm writing a recursive function that loops over a list of lists. This is the header of the function: void findDirToDelete(Directory*& current_dir_ptr, Directory*& dir_to_delete, int ...
StefanoSavarino's user avatar
0 votes
1 answer
52 views

Recursive Backtracking C++, Memory address changing, damaging pointers

I am currently working on a recursive backtracking method for maze generation. At the moment I am struggling to get the correct values from the GetNeighbours() function. Within the function, it ...
HarrisonO's user avatar
0 votes
1 answer
57 views

Returning reference to polymorphic object

I've read that references to object retain polymorphic behaviors, with that in mind I've set out to build an event framework based on const references passed in function calls and various callbacks, ...
Raff89's user avatar
  • 405
-2 votes
1 answer
59 views

binding reference of type 'type' to value of type 'const type' drops 'const' qualifier

This issue looks like known one. but i have no any const in types. What I am missing? I have the following code: class PlanForBlock { public: void registerExtraIterator(DummyLoadIterator *...
YAKOVM's user avatar
  • 9,911
0 votes
1 answer
35 views

Symfony ObjectProphecy treats array and object differently - can't change array which was initialised in setUp method

Related to this I am trying out this test code <?php use Monolog\Test\TestCase; use Prophecy\Prophecy\ObjectProphecy; use Prophecy\Prophet; class MyBar { public int $bar = 0; } class MyFoo { ...
user_51's user avatar
0 votes
3 answers
65 views

Realloc gives error: _CrtIsValidHeapPointer(block)

I have this school assignment where we are given some code to modify. The part im having trouble with is to truncate a dynamically allocated string. We are writing a function where we take in a ...
Windroz's user avatar
  • 13
1 vote
1 answer
46 views

Pass list of arguments to ggstatsplot function

I am using ggstatsplot to make lots of similar plots but the exact data or variable plotted will be different between each plot. I want to assign most of the (constant) arguments to a variable that I ...
Nathan Stutzman's user avatar
0 votes
2 answers
54 views

How to pass DOMDocument to Subroutine

I have a bunch of so called "structured data" from the German commercial register which comes in xml-files (one file always holds the data of one company). The data structure corresponds to ...
LuPi1801's user avatar
0 votes
1 answer
68 views

Add value to array in Symfony TestCase class in test function if the array was initialised in setUp method

Apologies if the question looks off, I am new to both Stack Overflow and php. My class looks a bit like this: final class MyClass extends TestCase { private MyService $myService; private ...
user_51's user avatar
0 votes
1 answer
47 views

Passing constexpr member as a reference triggers linker error with gcc in debug or -O1 modes

The code below triggers error with gcc 'undefined reference Foo::d' and 'undefined reference Foo::i'. Strangely, it happens only if I compile it in -Og, or -O1 optimization mode, but not with -O2 or -...
one_two_three's user avatar
17 votes
1 answer
2k views

If arrays are passed by reference, why should I use int(&)[]? [duplicate]

Consider: #include <iostream> using namespace std; void Change(int arr[3]) { for (int i = 0; i < 3; i++) { arr[i] = 1; } } int Test() { int arr[3] = { 0, 0, 0 }; ...
Francesco Derme's user avatar
1 vote
3 answers
63 views

Specialize variadic template to skip non-reference parameters

I am writing a wrapper for some client-server function calls, and I need a way to set some output parameter before or after the actual call. Sample code follows: #include <iostream> #include <...
G B's user avatar
  • 2,973
0 votes
0 answers
43 views

lvalue reference and rvalue binding

#include <string> #include <iostream> using std::string; using std::cout; using std::endl; void modify_nd_print_string(string&); int main (){ auto s = ""; ...
Vinod's user avatar
  • 1,041
0 votes
2 answers
82 views

how to use sizeof() for reference_to_array in c++ [duplicate]

How to use sizeof() to determine the size of a reference to an array? I have declared an array in main() and used sizeof() to print its total size it has occupied. And then I pass the array to a ...
semicolon_missing's user avatar
0 votes
0 answers
56 views

Why is my child_dirs std::list not retaining the objects that are added to it?

I’m a long time C# developer trying to learn C++, and I’m writing a program to recursively process a folder structure to build a hierarchy of Directory objects. BTW, I’m aware of ...
stevoh's user avatar
  • 53
1 vote
3 answers
111 views

How to pass an argument to a script by ref in bash

Please notice that I'm talking about passing arguments by-ref to a script, not to a function. I already checked here and here and they discuss about passing arguments using functions. What I want is ...
Adriano_Pinaffo's user avatar
0 votes
1 answer
49 views

Using reference type variables

It could be that it is nearly 5 pm on a Friday, but I am having trouble learning how to use [ref] type variables in PowerShell. The following prints 'NotTest' [ref]$refString = 'Test' $string = $...
Philosophene's user avatar
0 votes
2 answers
247 views

Weird behavior with std::string reference class member

Given this code: #include <iostream> class Foo { public: Foo(const std::string& label) : label_(label) {} void print() { std::cout << label_; }...
morpheus's user avatar
  • 19k
-2 votes
1 answer
129 views

Is passing value by reference thread-safe?

I have a following code: #include <thread> void foo(int& value) { // do nothing } int main() { int value = 42; std::thread t1([&value]{ foo(value); }); std::thread t2([&...
mouse_00's user avatar
  • 623
2 votes
1 answer
55 views

C++: "No match for 'operator<<' (operand types are 'std::ostream' and 'Complex')"

I'm trying to compare and print two Complex objects using a custom Max function in C++. The Complex class has overloaded operator>> and operator<<, and I have also defined a bool operator&...
Tahmid Kawser Washee's user avatar
1 vote
1 answer
46 views

Why am I receiving a heap corruption error in IntegerSet class?

I have an assignment in which I have to create a class called IntegerSet. The point is that it creates sets of integers. If an integer is present in the set, then that number has a 1 in its place in ...
Joe Forsyth's user avatar
1 vote
3 answers
84 views

C program calling malloc results in bus error?

I have written this code to declare a pointer, p, and then dynamically allocate 4 bytes of memory for type int to then store some data in it. p should point to an address which holds the data 6 of ...
tekkyprograms's user avatar
2 votes
2 answers
152 views

Why does the "in" keyword allow mutation of properties C#?

I am currently working on a function where an object (of self written type Catalog) is passed and shall not be mutated / changed in the called function. Here a quick example. public override bool ...
Patrick Cerny's user avatar
-3 votes
2 answers
49 views

Does the original list decrease in size when I convert a list to a queue and poll?

Assume I have an array list like List<Integer> list = [1,2,3,4]; // ignore the syntax here Then I convert the collection to a queue Queue<Integer> queue = new LinkedList(list); Then I do ...
user9341578's user avatar
0 votes
0 answers
24 views

How to avoid passing by reference inside of method (Java)? [duplicate]

I'm making my own Java matrix program for a linear algebra class and can't seem to stop passing my arrays by reference. I want each method to return a matrix (thus performing calculations based on the ...
Aidan K's user avatar
0 votes
3 answers
47 views

c++: why can't I see changes of an object variable, when I set it in the constructor and the object changes the value of it

Maybe it's better to show the code then it is better to understand what my problem is. Class: Cls::Cls() {} Cls::Cls(int &var) : m_var(var){ std::cout << "constructor: " <&...
user3137385's user avatar
0 votes
1 answer
24 views

How can I achieve pass-by-reference in Pike?

anyone who can help and is familiar with the Pike language. I am currently studying the Pike language and I would like to implement a void function that calculates the factorial of a given number &...
SwFly's user avatar
  • 35
-1 votes
2 answers
570 views

Why did this python list appear to get passed "by value"? [duplicate]

I defined the following function: def myFunction(a): print(f'In function, initial a = {a}, {id(a)}, {type(a)}') a = a*10 print(f'In function, final a = {a}, {id(a)}, {type(a)}') return a When ...
Kanika's user avatar
  • 1
0 votes
2 answers
58 views

"While loop" (contains pointer to array passed into a function) works only once

I am writing some code in C. Inside the main function there is a while loop with a function call to printboard taking a pointer to array board. Inside the function I have got a printf, that prints 3 ...
Filip Dvorak's user avatar
0 votes
1 answer
49 views

Can i optimize my Javascript class by only running reducer once, but keep the desired functionality?

Note: After working with react, redux, react-redux, redux-saga, redux-thunk, with and without typescript, I am trying to create a new and better framework for frontend async logic, with the benefits ...
johann1301s's user avatar
0 votes
1 answer
19 views

Some Issues in Tree Iteration in Python while the Tree Can be Dynamically Generated on the Fly

I am writing the following functionality. A root nodes is given with 2 set of indices (_indices_lhs and _indices_rhs), and fore every index that apperas in both the left hand side and right hand side ...
Cherry Toska's user avatar
-5 votes
1 answer
121 views

What happens at the compiler level when I dereference a pointer to pass it by reference? [closed]

I'm interested in what the compiler does when I dereference a pointer explicitly to pass it by reference: void foo(A& arg) { arg.member = 7; } void goo() { A* ob = new A(); foo(*ob); ...
Omroth's user avatar
  • 903
0 votes
1 answer
145 views

Sort Dictionary by Reference? (Python 3.10)

I'm attempting to sort a dictionary in Python in ascending key order. I'm trying to do this inside of a function, with the dictionary being supplied as an argument. I understand that there are mutable ...
Runsva's user avatar
  • 509
-3 votes
2 answers
56 views

Getting segmentation fault while using call by reference to return multiple values in recursive function

The question is to find the maximum and minimum element in an array and return their difference. I referred to the editorial of the problem on GeeksforGeeks. I used the tournament method, in which we ...
user22021739's user avatar

1
2 3 4 5
91