DEV Community

Discussion on: Why use pointers at all.

 
jessekphillips profile image
Jesse Phillips

I am taking words from the C spec when talking about C. I'm taking words from the Java spec when talking about Java. You are the one taking words from the C++ spec when talking about languages that are not C++.

Thread Thread
 
pentacular profile image
pentacular

With that put down, we can now utilize language from the C spec that says, the int is passed by reference to foo.

Are you still claiming that in C passing the value of a pointer to an int object to a function is passing the int object by reference?

Thread Thread
 
jessekphillips profile image
Jesse Phillips • Edited

"All interactions with objects and array is by reference, much like pointers in C."

Does this mean Java is pass by reference?

Is this statement wrong for Java?

Why won't you answer my questions for clarifying our disagreement?

Thread Thread
 
pentacular profile image
pentacular

Let's write it out properly.

"All interactions with objects and arrays is by reference values, much like pointers in C."

This does not mean that Java is pass-by-reference.

Reference values in Java are values and are passed by value like any other value in Java.

This is easy to test, since modifying a parameter in Java does not affect the corresponding argument.

It means that like pointers, they are not references, but can be operated upon to access an object indirectly.

In C, *p is used to access the object that pointer p points at.

In Java, p.x is used to access member x of the object that p refers to (if p happens to refer to an object, that is).

Thread Thread
 
jessekphillips profile image
Jesse Phillips • Edited

"The reference values (often just references)"

The Java spec has defined use of the word "reference" to mean "reference value?"

Feel free to disagree with the Java spec. I needed to know if when we read something we can come to the same conclusion about what it says. And that if we discuss things on an agreed upon definition it does not change because a different definition exists for the same term and is better suited for the argument at hand.

Thread Thread
 
pentacular profile image
pentacular

The Java spec has defined "reference values" and then said that "reference value" is often abbreviated as "references".

This is what "The reference values (often just references)" means.

Which means that when you read "references" in Java, you need to mentally expand it back out to "reference values".

If you don't do this, you'll end up making errors like claiming that Java supports pass-by-reference.

Thread Thread
 
jessekphillips profile image
Jesse Phillips • Edited

That sounds more like a no to me. Should have chosen yes.

Since it appears that we can choose our preferred definition with no authority on the matter, I choose Wikipedia.

Are you still claiming that in C passing the value of a pointer to an int object to a function is passing the int object by reference?

Yes.

Feel free to change my mind by updating the Wikipedia page. I'm sure it will go through unquestioned with your references.

Thread Thread
 
pentacular profile image
pentacular

If you believe that C has pass-by-reference semantics, there's really no point continuing the conversation.

Best of luck.

Thread Thread
 
jessekphillips profile image
Jesse Phillips

That isn't what you asked.

Thread Thread
 
pentacular profile image
pentacular

That isn't what you asked.

This is what I asked

Are you still claiming that in C passing the value of a pointer to an int object to a function is passing the int object by reference?

This is what you answered

Yes.

Passing by reference requires pass-by-reference semantics.

So, it is what I asked.

Do you believe that C have pass-by-reference semantics?

Thread Thread
 
jessekphillips profile image
Jesse Phillips

If you pass an address to a function then you have passed the int by reference.

C does not have pass-by-reference semantics and will not pass int by reference.

Java interacts with objects by reference. When you call a method you pass the object by reference.

Java does not have pass-by-reference semantics and will not pass an object reference by reference.

Thread Thread
 
pentacular profile image
pentacular

Passing an int * in C does not pass an int.

Thread Thread
 
jessekphillips profile image
Jesse Phillips • Edited

Right you're passing a reference to int.

Thread Thread
 
pentacular profile image
pentacular

No, you're passing the value of an int *.

Thread Thread
 
jessekphillips profile image
Jesse Phillips

You're spending so much effort being right rather than communicate.

You're trying to prove that this scotsman is not a true scotsman. And fail to recognize that he is in fact a scotsman.

Rather than directly addressing the definition of reference. You run me in circles. First by saying I needed to use the words of the spec, then by saying those words aren't the same in this other spec.

I pull in a third-party which you completely ignore, probably because it doesn't match your true scotsman. You try to show me differences, even though I clearly demonstrated an understanding of the different semantics.

You can only accept being right and have no interest in communicating.


Had you accepted the words used in the Java spec I could have answered No to the question about the behavior of C.

C did use the word reference, but it was not trying to define a language construct, instead it was using the word to help explain the semantics of a pointer, this matches the definition provided by Wikipedia.

Java on the other hand is defining a language construct. It has different semantics then C++'s use with the same name.

So we have reference as a concept, reference as a language construct in Java, and reference as a language construct in C++. Thus in order to communicate we have to agree to which of these we mean.

I thought that you would use the one defined in the language spec since that is what requested at the start. When I realized you were not sticking to that requirement, I was able to pull in more language into the mix and see how you would contort your logic to make it seem that all of these uses are consistent (they are not). But I was sadly disappointed when you wouldn't even touch Wikipedia definition.

Thread Thread
 
pentacular profile image
pentacular

Since we are talking about pass-by-reference, it is the semantics of pass-by-reference that are significant.

If your definition of 'reference' is 'something that can be used to find something', then there will be nothing that is not a reference.

Have a string? I can use that to reference something .: strings are references!

Have an integer? I can use that to reference something .: integers are references!

Have anything? I can use that to reference something .: anything is a reference.

Which is the path down which you are going by claiming that pointers are references, because they can reference things, and therefore passing a pointer to something is passing that something by reference.

It's meaningless -- which is why I have kept bringing it back to the semantics required by pass-by-reference.

Thread Thread
 
jessekphillips profile image
Jesse Phillips

And why I started with your ask, to use the Java spec which does not define them as you have here. My point was that we never established an agreed and consistent definition for reference.

I never once saw you pull out a definition from your sources. Even definition I had to dig for and you would deflect because it wasn't the one you wanted.

Thread Thread
 
pentacular profile image
pentacular

And I talked about the Java spec, which defines "references values" (abbreviated sometimes to references).

These are not references in the sense of pass-by-value, as I've demonstrated many times above.

You might claim that they're references in the sense that anything can be used to refer to something else.

But that won't get you references with the semantics required by pass-by-reference, which means that you can't claim that passing one is pass-by-reference.

Let's just imagine that they were called "oranges" -- would you be claiming that passing an "orange" gave you pass-by-reference?

If not, then it's clear that this is simply due to being confused by a name choice.

Thread Thread
 
jessekphillips profile image
Jesse Phillips • Edited

Let's try it.

"reference values (often just oranges)"

"All interactions with objects and array is by oranges, much like pointers in C."

Yep, I'd use it.

"Objects are pass by oranges and if C had them, it too would be pass by oranges."

Thread Thread
 
pentacular profile image
pentacular

Ok, and does pass-by-orange have pass-by-reference semantics, or does it have pass-by-value semantics? :)

Thread Thread
 
jessekphillips profile image
Jesse Phillips • Edited

Have we defined what we are discussing as passed and the definition of reference yet?

Thread Thread
 
pentacular profile image
pentacular

I've done so several times.

Here is the simplest requirement, again.

When passing by reference, modifying the parameter modifies the argument.

void foo(int &a) {
  a = 2;
}

int main() {
  int i = 1;
  foo(i);
  cout << i << endl;
  // Outputs 2.
}
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
jessekphillips profile image
Jesse Phillips

Under that definition, no passing by oranges does not have the same semantics as pass by reference.

This however changes the subject to the parameters of the functions. I have been talking about the object for which the parameter refers. Could we stick with talking about the concept I'm actually trying to get across rather than introducing a introducing a different one.

Since pass by oranges is not a term used in the Java spec, could you describe the pass by oranges semantics using spec terms?