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
9
votes
2 answers

Trying to use templatised fuctions to swap two strings

#include #include template void swap(T a , T b) { T temp = a; a = b; b = temp; } template void swap1(T1 a , T1 b) { T1 temp = a; a = b; b = temp; } int main() { int a = 10 , b = 20; …
9
votes
2 answers

Is There a Reason Standard Algorithms Take Lambdas by Value?

So I asked a question here: Lambda Works on Latest Visual Studio, but Doesn't Work Elsewhere to which I got the response, that my code was implementation defined since the standard's 25.1 [algorithms.general] 10 says: Unless otherwise specified,…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
9
votes
4 answers

Should a std::string class member be a pointer?

And why/why not? Say I have a class which takes a string in the constructor and stores it. Should this class member be a pointer, or just a value? class X { X(const std::string& s): s(s) {} const std::string s; }; Or... class X { …
Mistodon
  • 627
  • 1
  • 5
  • 12
9
votes
2 answers

Passing vector of vectors to function

I have a function which accepts a vector> and modifies the MyClass instances. It's been a long time since I've written any C++ and I'm having difficulty remembering what is sufficient here for passing the entire arg by reference…
9
votes
2 answers

Operator overloading by value results in use of moved value

Compiling the following Rust code that uses operator overloading use std::ops::{Add}; #[derive(Show)] struct Point { x: int, y: int } impl Add for Point { type Output = Point; fn add(self, other: Point) -> Point { Point…
zgerd
  • 1,080
  • 8
  • 17
9
votes
1 answer

Input parameter passing: is there a size threshold for efficient pass-by-value?

In C++, when an input argument is cheap to copy (e.g. like an int, float, etc.), it's usually passed simply by value. Instead, input "observed" arguments that aren't cheap to copy (e.g. std::string) are passed by const &. I was wondering about types…
Mr.C64
  • 41,637
  • 14
  • 86
  • 162
9
votes
6 answers

Assigning to a new value to an object passed into a function in JavaScript

I'm new to JavaScript (though experienced in C++), and today, I wrote something like this: function foo(bar) { bar = "something else"; } var x = "blah"; foo(x); alert(x); // Alerts with "blah", but I was expecting it to alert with "something…
oggmonster
  • 4,672
  • 10
  • 51
  • 73
9
votes
5 answers

JS object copy by value vs copy by reference

I was playing with chrome console and noticed something which I couldn't understand. I know in JS variables are copied by value and objects are copied by reference. Below code works fine as expected which outputs 2 and proves JS objects work as…
RuntimeException
  • 1,135
  • 2
  • 11
  • 25
8
votes
4 answers

Object by Reference vs. Reference by Value

I read this comment here: Passing a String by Reference in Java? Yes, it's a misconception. It's a huge, widespread misconception. It leads to an interview question I hate: ("how does Java pass arguments"). I hate it because roughly half of the…
Steve the Maker
  • 611
  • 6
  • 11
8
votes
4 answers

Immutable and pass by value

I have the following code which has a mutable Person class, String and a method to modify the instances of String and Person class Person{ int a = 8; public int getA() { return a; } public void setA(int a) { this.a =…
8
votes
6 answers

Properties - by value or reference?

I've got the following public property which exposes an Arraylist: public ArrayList SpillageRiskDescriptions { get { return _SpillageRiskDescriptions; } set { …
m.edmondson
  • 30,382
  • 27
  • 123
  • 206
8
votes
15 answers

Can someone explain to me what the reasoning behind passing by "value" and not by "reference" in Java is?

I'm fairly new to Java (been writing other stuff for many years) and unless I'm missing something (and I'm happy to be wrong here) the following is a fatal flaw... String foo = new String(); thisDoesntWork(foo); System.out.println(foo);//this prints…
Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236
8
votes
2 answers

What do "value semantics’" and "pointer semantics" mean in Go?

What is the meaning of Value semantics and Pointer semantics in Go? In this course, the author used to mention many times about above terms when explaining internals of arrays and slices which I couldn't understand it completely.
8
votes
1 answer

Passing structs by-value in LLVM IR

I'm generating LLVM IR for JIT purposes, and I notice that LLVM's calling conventions don't seem to match the C calling conventions when aggregate values are involved. For instance, when I declare a function as taking a {i32, i32} (that is, a struct…
Dolda2000
  • 25,216
  • 4
  • 51
  • 92
8
votes
1 answer

Why is a volatile local variable optimised differently from a volatile argument, and why does the optimiser generate a no-op loop from the latter?

Background This was inspired by this question/answer and ensuing discussion in the comments: Is the definition of “volatile” this volatile, or is GCC having some standard compliancy problems?. Based on others' and my interpretation of what should…
underscore_d
  • 6,309
  • 3
  • 38
  • 64