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

Complex numbers passed by-value from C++ to C does not seem to work on powerpc

When I'm passing a complex float(complex.h) from a c++ caller to a c library, the value does not pass correctly when running on a 32 bit power pc. I was using two different open source software libraries when I detected this problem. I've isolated…
dan
  • 119
  • 5
11
votes
3 answers

Passing objects by reference vs value

I just want to check my understanding of C#'s ways of handling things, before I delve too deeply into designing my classes. My current understanding is that: Struct is a value type, meaning it actually contains the data members defined…
Kyle Baran
  • 1,793
  • 2
  • 15
  • 30
11
votes
4 answers

Pass list to function by value

I want to pass a list into function by value. By default, lists and other complex objects passed to function by reference. Here is some desision: def add_at_rank(ad, rank): result_ = copy.copy(ad) .. do something with result_ return…
dondublon
  • 701
  • 1
  • 6
  • 13
11
votes
1 answer

How to access variable by id?

Possible Duplicate: Get object by id()? >>> var = 'I need to be accessed by id!' >>> address = id(var) >>> print(address) 33003240 Is there any way to use address of var in memory, provided by id(), for accessing value of var? UPD: I also want…
Gill Bates
  • 14,330
  • 23
  • 70
  • 138
10
votes
5 answers

template pass by value or const reference or...?

I can write a templated function this way template void f(T x) {…} or this way template void f(T const& x) {…} I guess that the second option can be more optimal as it explicitly avoids a copy, but I suspect that it can also fail…
Roman L
  • 3,006
  • 25
  • 37
10
votes
2 answers

Copy-on-modify semantic on a vector does not append in a loop. Why?

This question sounds to be partially answered here but this is not enough specific to me. I would like to understand better when an object is updated by reference and when it is copied. The simpler example is vector growing. The following code is…
JRR
  • 3,024
  • 2
  • 13
  • 37
10
votes
2 answers

Is there a point to define move-only objects in c++11?

I had a question about using unique-ptrs before. I get this answer recommending to use move-only objects. I defined a class as below: class B { const string objName; public: B ( B && ) = default; B & operator= ( B && ) = default; B…
Govan
  • 2,079
  • 4
  • 31
  • 53
10
votes
4 answers

How can I change Integer value when it is an argument like change array's value?

public static void main(String[] args) { Integer i = new Integer(0); int[] arr = {1}; p1(i); p2(arr); System.out.println(i); System.out.println(arr[0]); } public static void p1(Integer i) { i = 2; } public static void…
cow12331
  • 245
  • 2
  • 3
  • 9
10
votes
1 answer

Why is Java pass by value only?

So here is another worthy downvote question. I understand that Java IS pass by value and what this means and how it works. So this is not another can you explain what pass by value is. I am more curious as to WHY Java does not include pass by…
Danrex
  • 1,657
  • 4
  • 31
  • 44
10
votes
4 answers

Why are objects automatically passed by reference?

I have a general question about deep- and shallow-copy in the context of the pass-by-reference- and pass-by-value-concept of C#: In C# it is a requirement to explicitly create methods that accept pointers/references to be able to pass such to the…
Florian R. Klein
  • 1,375
  • 2
  • 15
  • 32
10
votes
2 answers

Is Objective-C pass-by-value or pass-by-reference?

Since we always use pointers to define variables, I was wondering if Objective-C is "pass by value", since like Java, the actual value would be passed by using its reference. However, since it seems to be built up on top of C, would it have all the…
gran_profaci
  • 8,087
  • 15
  • 66
  • 99
10
votes
4 answers

Pass by reference more expensive than pass by value

Is there a case where pass-by-reference is more expensive than pass-by-value in C++? If so, what would that case be?
erelender
  • 6,175
  • 32
  • 49
10
votes
7 answers

Any differences between f(const string &) and f(const string )?

class mystring { friend ostream& operator<<(ostream &out, const mystring ss) { out << ss.s; return out; } private: string s; public: mystring(const char ss[]) { cout << "constructing mystring : " << ss << endl; …
Jichao
  • 40,341
  • 47
  • 125
  • 198
10
votes
1 answer

Pass by value or universal reference

I want to develop a small polymorphic class with type erasure and I wonder which version of the templatized constructor is better and should be used. We can pass by value: class A { ... template< typename T > A( T t ) { /* create the…
10
votes
5 answers

Are Python's bools passed by value?

I sent a reference to a bool object, and I modified it within a method. After the method finished its execution, the value of the bool outside the method was unchanged. This leads me to believe that Python's bools are passed by value. Is that true?…
Geo
  • 93,257
  • 117
  • 344
  • 520