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

Capturing reference as value in Lambda expressions

int TestMethod(const std::map& map) { auto m1 = [map=map]() { // is map here captured as reference or value ? std::cout << map.at("test") << std::endl; return 1; }; return m1(); } Inside m1, is map…
rstr1112
  • 308
  • 4
  • 13
-1
votes
4 answers

Java Object Reference and Java Methods

I am unable to understand how this works public void addToRule(Rule r) { if (!getRuleList().contains(r)) { getRuleList().addElement(r); } } If I run this code: obj.addToRule(r); System.out.println(getRuleList().contains(r)); it…
MozenRath
  • 9,652
  • 13
  • 61
  • 104
-1
votes
1 answer

Corrupted size vs. prev_size in c with dynamic input function

I wrote a function to dynamic input a string in c but when i run the program and i write something whit more than about 15 char i get "corrupted size vs. prev_size" i don't understand why. int main() { char *text = malloc(sizeof(char)); …
-1
votes
3 answers

What is wrong in passing pointer variable as an function argument instead of address of variable in C?

I know that if I pass &a as an argument in fun ,I will get result as *p =20 after fun is executed .Now see in the following code p is pointer variable passed as an argument to function fun. But after compilation I get result as *p =10 instead of 20…
-1
votes
1 answer

bash - pass by value with no correct result

My code is the following: vpnname="test vpn" booleanCheck(){ echo $1 if (statement) then sendMail $1 } sendMail(){ mailsubject = "subject test for - $1" } if ! ping 8.8.8.8 then then checkIfSendEmail…
Kahn Kah
  • 1,389
  • 7
  • 24
  • 49
-1
votes
2 answers

Why do we need to pass an address value to an argument that accepts double pointers?

#include int malloc2(int **a) { *a = malloc(sizeof (int)); return 0; } int main() { int* b = NULL; malloc2(&b); } If an argument only accepts double pointers, how can it accept an address value?
Salty
  • 21
  • 5
-1
votes
2 answers

Pass by reference - "hack" in java not working anymore?

I tried to simulate pass-by-ref in java by passing an array of size 1 that contains the value to a corresponding method. The source: public static void test(String... a) { a[0] = new String("bar"); } public static void main(String[] args)…
NHSH
  • 69
  • 7
-1
votes
2 answers

please help me clarify C++ pass by value?

struct rec { int length; int breath; }; int area(rec objrec1) { objrec1.length ++; //objrec1.breath; //int result= objrec1.length++ * objrec1.breath; return(objrec1.length++ * objrec1.breath); //return(result); } int…
Lionel
  • 11
  • 2
-1
votes
3 answers

Array is not erased after end of function call

in school the teacher told me that when the call to a function reaches an end everything declared inside the function's block will be erased. But I wrote the following code: int * secret() { int arr[10]={0}; arr[0]=9999; return…
user12603566
-1
votes
1 answer

How do I prevent a copy of a grid's datasource from not getting changed when the grid's data changes?

This is a C# Windows Forms project. I have a grid on a form. To get the data for the grid, I run a SQL procedure and store the results in a class. I want to have a copy of that class so I know what the values were before the user changes them in…
boilers222
  • 1,901
  • 7
  • 33
  • 71
-1
votes
2 answers

C ++ pass by reference or pass by value?

So I thought that C++ works by value, except if you use pointers. Although today I wrote this piece of code which works in a different manner than expected: #include using namespace std; void bubbleSort(int A[],int arrSize); bool…
Alex Lemesios
  • 522
  • 9
  • 22
-1
votes
1 answer

Why is x and y pass-by-value, not reference for the code below?

For the code below why are the variables x,y,s pass-by-value while only z is pass-by-reference. void foo(int* a, int* b, int& c, int d) { *a = *a + 1; b = b + 1; c = c + 1; d = d + 1; } int main() { int x = 0, y = 5, z = 10, s = 20; …
Sammy2000
  • 15
  • 6
-1
votes
1 answer

in c#, Why this code work as by reference? I intended to use as by value

I'm trying to make a list of class as history. So list of a class was declared like this. private List SearchHistoryList; SearchHistoryItem is a class that have two property. public DataTable SearchResultDataTable; public…
박영환
  • 99
  • 11
-1
votes
2 answers

Inout vs pass by reference swift optimization

I have a very large list of numbers and I would like to pass it to a function to do some manipulations to it. Originally, I created a function with an inout property. As many know, inout in swift does NOT pass by reference, rather it makes an…
EK_AllDay
  • 1,095
  • 2
  • 15
  • 35
-1
votes
2 answers

Why function won't modify this array

Function won't modify passed arr when calling this function: const animalTypes = [ new Animal( "wolf", "audio/wolf.mp3" ), new Animal( "frog", "audio/frog.wav" ), new Animal( "cow", "audio/cow.mp3" ), new Animal( "snake", "audio/snake.mp3"…
user3565923
  • 153
  • 2
  • 12