What is the main characteristic of pass-by-reference?

  • (11 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • Last »

154 Replies - 7308 Views - Last Post: 16 November 2017 - 08:22 AM

Poll: What is the main characteristic of pass-by-reference? (1 member(s) have cast votes)

What is the main characteristic of pass-by-reference?

  1. The ability of a method to use a reference(like a pointer or something similar to it) and operate on the original data/object without duplicating it (1 votes [100.00%])

    Percentage of vote: 100.00%

  2. The ability of a method to make the passed variable(argument) point to something else (including the ability to swap object or data) (0 votes [0.00%])

    Percentage of vote: 0.00%

Vote Guests cannot vote

#16 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13493
  • View blog
  • Posts: 53,901
  • Joined: 12-June 08

Re: What is the main characteristic of pass-by-reference?

Posted 24 October 2017 - 07:07 AM

Welcome to the world.
Language is super fuzzy.

Posted Image
Was This Post Helpful? 0
  • +
  • -

#17 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1378
  • View blog
  • Posts: 4,320
  • Joined: 20-March 10

Re: What is the main characteristic of pass-by-reference?

Posted 24 October 2017 - 07:21 AM

Hmmm,

why so angry at the world ?


what are you trying to prove ?

that you are smart ?

cos you dont seem so smart jimblumberg was right on the money and you seem confused about pointers and passing by reference......

listen to jim he makes a lot of sense.
Was This Post Helpful? 0
  • +
  • -

#18 ccdan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 107
  • Joined: 09-December 12

Re: What is the main characteristic of pass-by-reference?

Posted 24 October 2017 - 09:49 AM

View Postsnoopy11, on 24 October 2017 - 05:21 PM, said:

what are you trying to prove ?

Well, as you can probably see, this topic was started with a poll included - that is, I was trying to get c++ developers' opinions on a subject. I don't really see that as necessarily trying to prove anything.

View Postsnoopy11, on 24 October 2017 - 05:21 PM, said:

cos you dont seem so smart jimblumberg was right on the money and you seem confused about pointers and passing by reference......

listen to jim he makes a lot of sense.

Ohhhh... OK! Maybe I'm not so smart. Maybe. Not that it matters very much. In a debate, my little brain is used to evaluating the validity of the arguments instead of assessing the popularity or perceived "authority" of those who bring them.

"listen to jim" you're saying... OK. Let's see what jim said:

View Postjimblumberg, on 23 October 2017 - 05:39 PM, said:

Do you realize that in C or C++ everything is passed by value?

Well, that's quite an extraordinary statement! Why? My little brain thinks a bit like this:

Everything is black. Therefore nothing can be white. Or any other color. Because everything is black.
conversely, I get the following from jim's words:
In C or C++ everything is passed by value. Therefore nothing else is left to be passed by any other pass mechanism.

Sure, since you believe I'm not so smart, who knows, maybe my logic is flawed. I'd be glad to learn exactly where it is flawed.

On the other hand, if my logic solid, we're back to my first reaction: It's quite extraordinary to pretend that neither C nor C++ have any other passing mechanism other than pass-by-value. Most people would be baffled because almost all(if not all) C++ books and many C books talk about pass-by-reference when it comes to "stuff" passed to functions.

I don't know about you, but, from people who seem experienced in a certain area, I expect a bit more than "listen to jim" :)/>

This post has been edited by ccdan: 24 October 2017 - 09:51 AM

Was This Post Helpful? 0
  • +
  • -

#19 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10690
  • View blog
  • Posts: 18,307
  • Joined: 19-March 11

Re: What is the main characteristic of pass-by-reference?

Posted 24 October 2017 - 10:13 AM

I have no dog in this fight, but I'm going to ask you to dial back the snide to about a three. For reference, you're looking like you're at about a 9.5 now.
Was This Post Helpful? 0
  • +
  • -

#20 jimblumberg  Icon User is online

  • member icon

Reputation: 5336
  • View blog
  • Posts: 16,619
  • Joined: 25-December 09

Re: What is the main characteristic of pass-by-reference?

Posted 24 October 2017 - 11:04 AM

Quote

Well, that's quite an extraordinary statement! Why? My little brain thinks a bit like this:

Everything is black. Therefore nothing can be white. Or any other color. Because everything is black.
conversely, I get the following from jim's words:
In C or C++ everything is passed by value. Therefore nothing else is left to be passed by any other pass mechanism.


Then maybe you need to expand that "little brain".

Do you understand that in C++ a reference is basically functions the same as a pointer, with a few differences that make no difference in this content (basically a reference is just a wrapper around a pointer)?

Do you understand how hardware stacks and registers work?

Do you understand that passing arguments to functions in C or C++ uses either a register or a hardware stack?

So to pass something to a function you must copy something into either a register or a hardware stack. This copying the crucial part of my statement, what is actually copied and how it is used is what makes the difference between the different language methods (pass by value, pass by pointer reference, pass by reference (C++ only).

When passing by reference you are copying the address of the variable into either a register or a hardware stack, in other words you are passing the address by value.

Also I am not the only one that says this, look at this link. And from that link (highlight mine):

Quote

Argument passing

In C, arguments are passed to functions by value while other languages may pass variables by reference. This means that the receiving function gets copies of the values and has no direct way of altering the original variables. For a function to alter a variable passed from another function, the caller must pass its address (a pointer to it), which can then be dereferenced in the receiving function. See Pointers for more information.


Jim
Was This Post Helpful? 0
  • +
  • -

#21 ccdan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 107
  • Joined: 09-December 12

Re: What is the main characteristic of pass-by-reference?

Posted 24 October 2017 - 11:26 AM

View Postjimblumberg, on 24 October 2017 - 09:04 PM, said:

When passing by reference you are copying the address of the variable into either a register or a hardware stack, in other words you are passing the address by value.

Absolutely! Have I ever said anything else? Isn't that what I said in the other thread?

View Postccdan, on 18 June 2017 - 01:50 PM, said:

1 - Java is always pass by value, the address of the reference is being passed(copied) - Why the heck do we keep hearing such a statement? Is there any other way to achieve pass by reference other than by copying the address of an object into a method parameter? Is it possible to somehow "move" physical(or virtual) memory addresses into method parameters? Or is it possible to somehow move methods around physical memory addresses? I'm not aware of such a thing(and it doesn't even make sense to me.) If you know better, feel free to illuminate me, but until then I'll say this: if memory addresses can't be moved around, then they must copied somehow, more or less transparently. I don't think any language can achieve pass by reference other than by copying the addresses being referenced.


But that does't turn pass by reference into pass by value. Because the actual data the method processes is not that address, but rather whatever resides at that address. When we have pass by value, we have a copy of that entire thing(that resides at that address)

This post has been edited by ccdan: 24 October 2017 - 11:27 AM

Was This Post Helpful? 0
  • +
  • -

#22 jimblumberg  Icon User is online

  • member icon

Reputation: 5336
  • View blog
  • Posts: 16,619
  • Joined: 25-December 09

Re: What is the main characteristic of pass-by-reference?

Posted 24 October 2017 - 11:45 AM

Quote

Have I ever said anything else?

Sounded to me like you did.

Quote

Quote

View Postjimblumberg, on 23 October 2017 - 07:30 PM, said:
No its very relevant. Everything is passed by value, period. In this case the pointer/reference is what is copied.


Nonsense. If it's passed by value, then the content of the memory region the passed variable points to, is copied(duplicated), otherwise it's something other than pass by value.


Quote

Isn't that what I said in the other thread?

What other thread? The only other thread about this topic I'm aware about is the StackOverflow topic I linked to earlier. Since I don't frequent the Java sub-forum if it is a Java question in the Java sub-forum, as it appears I don't have any knowledge of that topic. Do you know many people consider it a bad practice to post the same question to multiple forums at the same time, especially when you don't link the relevant other topics?

Edit: Also remember that in my first post in this topic I stated:

Quote

I'll not comment on the Java aspects too much but I imagine the idea is much the same as in C or C++.

I'm not a Java programmer so I won't comment on the Java angle.


Jim

This post has been edited by jimblumberg: 24 October 2017 - 11:51 AM

Was This Post Helpful? 0
  • +
  • -

#23 ccdan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 107
  • Joined: 09-December 12

Re: What is the main characteristic of pass-by-reference?

Posted 24 October 2017 - 12:03 PM

View Postjimblumberg, on 24 October 2017 - 09:45 PM, said:

Sounded to me like you did.

Where?

View Postjimblumberg, on 24 October 2017 - 09:45 PM, said:

What other thread? The only other thread about this topic I'm aware about is the StackOverflow topic I linked to earlier[...] especially when you don't link the relevant other topics?

View Postjimblumberg, on 24 October 2017 - 09:45 PM, said:

Quote

I'll not comment on the Java aspects too much but I imagine the idea is much the same as in C or C++.

I'm not a Java programmer so I won't comment on the Java angle.

Given that your understanding and definition of pass-by-value is identical to what I hear when it comes to Java, it's about the same thing. I mentioned that in the first post(which you probably didn't pay attention to) and I also linked that topic:

View Postccdan, on 23 October 2017 - 03:33 PM, said:

My question comes as a result of the fact that the subject causes a lot of confusion in the Java world, which confusion was caused by James Gosling due to his claim that java "is always pass by value" (which is totally wrong in my opinion, I have a topic on that subject: http://www.dreaminco...alse-nonsense/)
While we don't know exactly what he was thinking, many people believe he said that in order to accommodate c++ developers' view of the subject, very often presented in c++ beginner books as the ability to swap what two variables point to. On the other hand, I'm almost sure that even c++ developers use pass-by-ref mainly in order to avoid duplicating data/objects(that can be pretty large) and not for swapping things.

What's your view on the subject?

This post has been edited by ccdan: 24 October 2017 - 12:04 PM

Was This Post Helpful? 0
  • +
  • -

#24 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5899
  • View blog
  • Posts: 20,141
  • Joined: 05-May 12

Re: What is the main characteristic of pass-by-reference?

Posted 24 October 2017 - 06:52 PM

Let's start off with agreeing that C is always pass by value. Any time we say we are doing pass by reference in C what we are actually taking about is passing a pointer. The pointer is simply a value that contains the address of what we want to change. That address is passed by value.

Early C++ compilers were actually not true compilers as we use the term nowadays. They were actually C++-to-C translators. Back then when you "compiled" a C++ program, the "compiler" would translate the C++ code to C, and then invoke the regular C compiler to compile the C code. (If you have ever heard the term "C++ name mangling", this is part of the origin of the term. The C++ compiler would change some of the C++ function names to incorporate information about what class/struct a function belongs to and what parameters it takes.)

So since C is pass by value, the original C++ is also pass by value.

But wait! What about the C++ references and reference parameters? What happens to code that looks like this?
void swap(int & left, int & right)
{
    int temp = left;
    left = right;
    right = temp;
}

int main()
{
    int x = 1;
    int y = 9;
    swap(x, y);

    int foo = 42;
    int & bar = foo;
    bar = 7;

    return 0;
}



Although there is more rigor to what the C++ compiler will let you do with references, they are essentially syntactic sugar for pointers. They just happen to be pointers that are guaranteed to point to someplace good (most of the time). So the compiler translates it into C code that uses pointers.

void swapIntRefIntRef(int * left, int * right)
{
    int temp = *left;
    *left = *right;
    *right = temp;
}

int main()
{
    int x = 1;
    int y = 9;
    int foo = 42;
    int * bar = &foo;

    swapIntRefIntRef(&x, &y);

    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#25 ccdan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 107
  • Joined: 09-December 12

Re: What is the main characteristic of pass-by-reference?

Posted 24 October 2017 - 08:41 PM

View PostSkydiver, on 25 October 2017 - 04:52 AM, said:

Let's start off with agreeing that C is always pass by value.

That, I'm afraid, is the original source of disagreement.

View PostSkydiver, on 25 October 2017 - 04:52 AM, said:

Any time we say we are doing pass by reference in C what we are actually taking about is passing a pointer. The pointer is simply a value that contains the address of what we want to change. That address is passed by value.


I don't think we all agree with that view. While we also pass a pointer with an address that's been copied, that's just technical detail that gets us to the real thing of interest that's being passed: the actual data present at that address.

We have very different interpretations of the word "pass" in "pass-by-whatever"

When I(and many other people) say that something is passed by reference, I always refer to the data present at the address indicated by the reference and never to the reference itself, because it wouldn't make any sense at all. After all, it's pass-by-reference, not pass-the-reference, so we won't focus on the reference but rather on the data it points to.

In my view, pass-by-reference, represents the ability of a method to operate, at the request of a caller(the caller specifying which external data the method should access), on data that's been defined outside its scope.

On the other hand, pass-by-value, means duplicating the data a passed variable points to and making that content available within the scope of the reference. For example, if the original variable outside a function points to an image, and that variable will be passed to function with pass-by-value, then a new image with the same content will be created and the formal parameter of the function will point to it, so that the function has its own image to operate on locally, within its own scope.

View PostSkydiver, on 25 October 2017 - 04:52 AM, said:

But wait! What about the C++ references and reference parameters? What happens to code that looks like this?

C++ references are a very particular feature of the language and don't think it's very relevant to the whole discussion. My view is that "reference" in pass-by-reference, has a much more general meaning, it's largely synonymous to a pointer(something that holds an address)
Was This Post Helpful? 0
  • +
  • -

#26 jimblumberg  Icon User is online

  • member icon

Reputation: 5336
  • View blog
  • Posts: 16,619
  • Joined: 25-December 09

Re: What is the main characteristic of pass-by-reference?

Posted 24 October 2017 - 09:25 PM

Quote

I don't think we all agree with that view.

Yes you're correct with this statement since you and only you don't agree that everything in C is passed by value.

Quote

We have very different interpretations of the word "pass" in "pass-by-whatever"

No, you are the only one that seems to be confused by these terms.


Jim
Was This Post Helpful? 0
  • +
  • -

#27 ccdan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 107
  • Joined: 09-December 12

Re: What is the main characteristic of pass-by-reference?

Posted 25 October 2017 - 02:42 AM

View Postjimblumberg, on 25 October 2017 - 07:25 AM, said:

Yes you're correct with this statement since you and only you don't agree that everything in C is passed by value.

There are many authoritative sources that don't agree that "everything in C is passed by value."

Here are some excerpts:

Expert C Programming - Deep C Secrets (Peter Van Der Linden)

Quote

All non-array data arguments in C are passed "by value" (a copy of the argument is made and passed to the called function; the function cannot change the value of the actual variable used as an argument, just the value of the copy it has). However, it is potentially very expensive in memory and time to copy an array; and most of the time, you don't actually want a copy of the array, you just want to indicate to the function which particular array you are interested in at the moment. One way to do this might be to allow parameters to have a storage specifier that says whether it is passed by value or by reference, as occurs in Pascal. It simplifies the compiler if the convention is adopted that all arrays are passed as a pointer to the start, and everything else is passed by making a copy of it.

and a table from the same book:
Posted Image

Pointers On C - Kenneth Reek

Quote

The fact that a pointer is passed rather than at copy of the array is what gives arrays their call by reference semantics. The function can manipulate the argument as a pointer, or it can use a subscript with the argument just as with an array name
[..]
Thus, modifying an array element in the function actually modifies the
callerʹs array. This behavior is named call by reference. You can get call by reference
semantics with scalar arguments by passing pointers to the arguments and using
indirection in the function to access or modify the values
.


Pointers in C (Apress)

Quote

Pass by Reference
This is another technique of passing information among functions. Pass by reference is used to pass the memory address of variables rather than the value itself.


And there are countless other C books that mention "call by reference" or "pass by reference". A simple claim that "pass by reference" is somehow "simulated" doesn't help the "all is call/pass by value" cause very much - especially when non-simulated examples from other languages are not provided(so that people understand that there's a clear distinction between the two)

View Postjimblumberg, on 25 October 2017 - 07:25 AM, said:

No, you are the only one that seems to be confused by these terms.

And you fail every single time to show me exactly the technical details I'm confused about(eg. what is technically wrong in my posts)
Was This Post Helpful? 0
  • +
  • -

#28 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13493
  • View blog
  • Posts: 53,901
  • Joined: 12-June 08

Re: What is the main characteristic of pass-by-reference?

Posted 25 October 2017 - 07:35 AM

Does this change any of the actual usage of it?

Pass by value - what ever you change in a parameter - inside the function - stays there. Like Vegas.
Pass by ref - what ever you change in a parameter - inside the function - is carried outside of the function. Like the clap from Vegas.
Was This Post Helpful? 1
  • +
  • -

#29 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1378
  • View blog
  • Posts: 4,320
  • Joined: 20-March 10

Re: What is the main characteristic of pass-by-reference?

Posted 25 October 2017 - 04:18 PM

Do you have any Vegas experience ?
Was This Post Helpful? 0
  • +
  • -

#30 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,997
  • Joined: 09-June 09

Re: What is the main characteristic of pass-by-reference?

Posted 25 October 2017 - 05:34 PM

Pointers are passed by value, Non-pointers are passed by value ... everything is passed by value.

When we say a pointer is passed by value, we are saying that a copy of the pointer is being created, thus changes to that pointer address will not reflect when the function returns.

The pass by reference "lingo" is used to describe modifying data at the pointers dereference location. The pointer itself however is passed by value.

i.e.
void change(int *ptr) {
   ptr = NULL;
}

int main() {
   int x;
   int *y = &x;
   change(y);

   // Y != NULL here, that's because y was passed by value!


This post has been edited by jjl: 25 October 2017 - 05:36 PM

Was This Post Helpful? 0
  • +
  • -

  • (11 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • Last »