Questions tagged [pass-by-value]

pass-by-value is a "one way passing" so that modifications of the passed value inside the receiving function (or other entity like process, etc) are not returned back.

pass-by-value is a one way passing so that modifications of the passed value inside the receiving function (or other entity like process, etc) are not returned back.

pass-by-value normally means cloning a variable and making a cloned copy available to the receiving function. Modifications of this copy are allowed, but they are not returned back to the caller; the copy is simply disposed after the call returns.

Passing by value is more secure if the function can also be called by malicious code (malicious code can retain the reference and observe it later even if it cannot modify it). Some are also convinced it results a cleaner code. However passing of the larger structures by value also requires more resources.

1173 questions
-1
votes
1 answer

Dynamic realloc inside a function - C

I seem to have a problem with reallocating memory of arrays inside a function. Whenever the code gets to realloc, it is shut down due to Segmentation fault. I call malloc in main: char* coded_message = (char*) malloc(BASE_LENGTH *…
iandorr
  • 1
  • 4
-1
votes
1 answer

Mimicking Pass-By-Reference in Python/Javascript Using Wrappers - Good Practice?

Say I want to modify a number or some other primitive inside of a function. For example, something like this (note: pseudocode): // apply fn to every value in a tree, in-order traversal function treeReduce (tree, fn, result): if (tree ==…
woojoo666
  • 7,801
  • 7
  • 45
  • 57
-1
votes
1 answer

Using Pass by value or Pass by Reference?

I have two methods in my code. Below is one of them. private async void Characteristic_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args) { var newValue = FormatValueByPresentation(args.CharacteristicValue,…
Smit Modi
  • 55
  • 3
  • 13
-1
votes
1 answer

Working on my first real game in Python, But I've Ran into a few problems

While coding my game, I've ran into problems when running it. For example, the zombie attack doesn't come out correctly, when using the store the gold value and health value do not update. When running the battle part, the first time the zombie has…
-1
votes
1 answer

Is it undefined to return reference to a function-scoped variable as a value?

Is the code below legal? int foo() { int local = 5; int& local_ref = local; return local_ref; } If yes, then it's most likely will copy value of local to the return value before destroying local, but why? Neither GCC nor MSVC doesn't…
r5ha
  • 332
  • 5
  • 13
-1
votes
1 answer

Why does this pointer point to "Horse cult" instead of "Horse horse"?

1 public class Horse { 2 Horse same; 3 String jimmy; 4 5 public Horse(String lee) { 6 jimmy = lee; 7 } 8 9 public Horse same(Horse horse) { 10 if (same != null) { 11 Horse same = horse; 12 …
wokki
  • 9
  • 1
  • 4
-1
votes
1 answer

"Invalid procedure call or argument" when calling a sub without parenthesing one parameter

I wrote a COM-Visible DLL with this piece of code (VB.NET) ' .NET Class with implementing an interface to make it visible Public Class VisibleClass Public Sub callableSub(ByVal names As ACustomCollection, _ Optional…
Amessihel
  • 5,891
  • 3
  • 16
  • 40
-1
votes
1 answer

c#: after pass by value for a reference type, would changing the value outside affect the object?

In the below code I initialized the client and passed it to the constructor of the new Worker classes. I then set the value to null while the two workers are still running. To my understanding I'm doing a pass by value for reference type in the…
psbox2021
  • 43
  • 5
-1
votes
3 answers

Possible missunderstanding of how pass by value works

I'm not sure if I understand how pass-by-value works in Java. I know there are many topics about that, even on SO, but they all don't really fit and I'd like to know if I'm right: Lets say I've got the following Code: class Class{ public String…
Chris
  • 1
  • 1
-1
votes
1 answer

Trying to dereference an interface that is a pointer to a struct object on the back end so I can pass by value to a function

I have an interface that represents a pointer to a struct object after it has been initialized. I want to get access to the Value of the object that the interface references rather than the pointer so that I can pass it by value to a function as the…
-1
votes
1 answer

Pass by reference value not working in java

I am trying to write a program for quicksort using singly linked list in java. Below is the code. public class QuickSortInSLinkedList { Node head; private static class Node{ private int data; private Node next; Node(int data){ …
-1
votes
2 answers

int vs Integer vs a user made class to achieve effective pass by reference behavior in Java

Let's say you have a class of object with three integer fields that you want to possibly change, all in the same way, with one method. Let's keep it simple and say all that the method does is add 1 to the parameter passed to it. That is to say,…
Ryan McNames
  • 497
  • 4
  • 3
-1
votes
1 answer

Output for pass-by-value and pass-by-name

I am needing to find the result of the following code when x and y are passed-by-value and also when passed-by-name. PROGRAM EX1; int i; //global int A[3]; //global PROCEDURE P1(int x, int y) Begin y:=2; PRINT(x); …
-1
votes
3 answers

Java pass by value query

Since java is pass by value.In below code we are passing a value to appendStringMethod not a reference, then why in main method we get HelloWorld not just Hello after calling appendStringMethod() in main. public class test { public static void…
-1
votes
2 answers

How can a void sorting algorithm "return" a result?

public static void main(String[] args) { int sizeOfTestArray = 50; int[] testArray = new int[sizeOfTestArray]; Random random = new Random(); for (int i = 0; i < sizeOfTestArray; i++) { testArray[i] = random.nextInt(100); …
Patrick
  • 35
  • 4