473,466 Members | 1,457 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Pointer vs Reference


Can we confirm the following? also someone said, Java also has
"reference" like in C++, which is an "implicit pointer":

Pointer and Reference
---------------------

I am starting to see what pointer and reference are and how they
relate
to each other.

in the C era, a pointer *is* a reference. that's why when we have

int a = 10;
int *pi = &a;

and you can "dereference it":

*pi = 20;

Until when C++ comes along, then we have a new "reference":

int a = 10;
int i =& a; // or int i = &a; i am not sure about the syntax.
i = 20; // now both a and i are 20

so this type of reference is an implicit pointer... it points to a,
but
you don't use the way in C (int *pi = &a) And when you use (i = 20),
it does the dereference silently. (*pi = 20;)

so a reference is new: a pointer but "looks like not a pointer".

come to think about it, in Java and Ruby, they are like that too.

a = Car.new

a doesn't look like a pointer, but it is actually a pointer.

we don't dereference it to get to the attributes like (*a).value = 10
or a->value = 10 but just use a.value = 10

So from this point on, a reference and a pointer are not the same... a
reference is a pointer "that doesn't look like a pointer."

they both points to something. but the syntax (or grammar) of usage
doesn't look like it is a pointer in the C era. a reference is an
"automatically dereferenced" pointer, shall we say? or an "implicit"
pointer, or "silent" pointer.

in PHP5,

$a = 10;
$b =& $a; # now $b implicitly points to $a
$b = 20; # now $b implicitly points to $a, which is 20

$a = new Foo("hello"); # $a implicitly points to a Foo object
# the Foo object is 100 bytes,
# but $a is just 4 bytes

$b =& $a; # $b implicitly points to $a.
# $b is a pointer to pointer
# $b points to a four byte pointer, which is $a

$b = new Foo("ok"); # dereference $b and sets its content to
# a new pointer to another object Foo("ok")
# that is, $a points to Foo("ok") now
# $b still points to $a, which points to
Foo("ok")

So now, when you print $b and $a, they are both Foo("ok")
In language we use nowadays, which language has "reference"

In think C++, Java, (Perl?), Python, PHP5, Ruby all have reference.

In language we use nowadays, which language has "reference" to a
"reference"?

I think C++, Java, PHP5 do... not sure about Python and Ruby.

Sep 29 '07 #1
41 3598
Summercool wrote:
Can we confirm the following? also someone said, Java also has
"reference" like in C++, which is an "implicit pointer":

Pointer and Reference
---------------------

I am starting to see what pointer and reference are and how they
relate
to each other.

in the C era, a pointer *is* a reference. that's why when we have

int a = 10;
int *pi = &a;

and you can "dereference it":

*pi = 20;

Until when C++ comes along, then we have a new "reference":

int a = 10;
int i =& a; // or int i = &a; i am not sure about the syntax.
int i = &a; is the syntax. Yes, the ampersand is used for both reference
and address-of, so it can be confusing (especially since I think that i
would be set to the address of a in C).
i = 20; // now both a and i are 20

so this type of reference is an implicit pointer... it points to a,
I wouldn't call it an implicit pointer: you cannot, to my knowledge,
change i to point to any other object but a like you can with a pointer.
Rather, i is an alias of a: the two objects forever more point to the
same thing.

so a reference is new: a pointer but "looks like not a pointer".
Not quite. See above.
come to think about it, in Java and Ruby, they are like that too.
No, it is not. In syntax, Java more nearly follows the reference syntax
but it is closer to a pointer with transparent {de}referencing.
In language we use nowadays, which language has "reference"

In think C++, Java, (Perl?), Python, PHP5, Ruby all have reference.

In language we use nowadays, which language has "reference" to a
"reference"?

I think C++, Java, PHP5 do... not sure about Python and Ruby.
Let me start by asking a question to the C++ language lawyers out there:
what should this print out:

int a = 50, b = 20;
int i = &a;
std::cout << "a: " << a << " b: " << b << " i: " << i;
i = 30;
std::cout << "a: " << a << " b: " << b << " i: " << i;
i = &b;
std::cout << "a: " << a << " b: " << b << " i: " << i;
b = &a;
std::cout << "a: " << a << " b: " << b << " i: " << i;
a = &i; // Creating a circular loop?
std::cout << "a: " << a << " b: " << b << " i: " << i;

Java does not have double referencing or referencing as I understand the
C++ spec (I, unfortunately, no longer have my draft copy of the spec).
Every non-primitive type is closer to a pointer in JVM implementations
(double-pointers, actually, under most implementations). I think PHP5
tends to follow the C++ way. I don't know anything about Ruby and I
think that Python follows the Java style of what's going on.
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
Sep 29 '07 #2
Summercool wrote:
>
Can we confirm the following? also someone said, Java also has
"reference" like in C++, which is an "implicit pointer":
Java references are neither like C++ references nor like C++ pointers. They
are something in between.
in the C era, a pointer *is* a reference.
Depending on how you define "reference", you could say that.
Until when C++ comes along, then we have a new "reference":

int a = 10;
int i =& a; // or int i = &a; i am not sure about the syntax.
It doesn't matter. Whitespace before and after the & operator is ignored.
i = 20; // now both a and i are 20

so this type of reference is an implicit pointer... it points to a,
but you don't use the way in C (int *pi = &a) And when you use (i = 20),
it does the dereference silently. (*pi = 20;)

so a reference is new: a pointer but "looks like not a pointer".
A reference in C++ is another name for an already existing object. After its
initialization, it behaves just like the object it refers to.
So from this point on, a reference and a pointer are not the same... a
reference is a pointer "that doesn't look like a pointer."

they both points to something. but the syntax (or grammar) of usage
doesn't look like it is a pointer in the C era. a reference is an
"automatically dereferenced" pointer, shall we say? or an "implicit"
pointer, or "silent" pointer.
No, it isn't. A pointer can be null, a reference can't. A pointer can be
uninitialized, a reference can't.
In language we use nowadays, which language has "reference" to a
"reference"?
That's another thing you can't have in C++. You can have a pointer to
pointer, but you can't have a reference to reference, because references
are not objects (unlike pointers).
Sep 29 '07 #3
"Joshua Cranmer" <Pi*******@verizon.netwrote in message
news:VUtLi.1941$1d2.688@trndny05...
Summercool wrote:
>Can we confirm the following? also someone said, Java also has
"reference" like in C++, which is an "implicit pointer":

Pointer and Reference
---------------------

I am starting to see what pointer and reference are and how they
relate
to each other.

in the C era, a pointer *is* a reference. that's why when we have

int a = 10;
int *pi = &a;

and you can "dereference it":

*pi = 20;

Until when C++ comes along, then we have a new "reference":

int a = 10;
int i =& a; // or int i = &a; i am not sure about the syntax.

int i = &a; is the syntax.
ITYM int& i = a;

Best wishes,
Stu

<snip>
Sep 29 '07 #4
On 29/9/07 16:09, in article VUtLi.1941$1d2.688@trndny05, "Joshua Cranmer"
<Pi*******@verizon.netwrote:
Summercool wrote:
>Can we confirm the following? also someone said, Java also has
"reference" like in C++, which is an "implicit pointer":

Pointer and Reference
---------------------

I am starting to see what pointer and reference are and how they
relate
to each other.

in the C era, a pointer *is* a reference. that's why when we have

int a = 10;
int *pi = &a;

and you can "dereference it":

*pi = 20;

Until when C++ comes along, then we have a new "reference":

int a = 10;
int i =& a; // or int i = &a; i am not sure about the syntax.

int i = &a; is the syntax. Yes, the ampersand is used for both reference
and address-of, so it can be confusing (especially since I think that i
would be set to the address of a in C).
Wrong. I think what you probably meant is

int& i = a;

Yes, the ampersand is used for reference and address-of - when used as part
of a type, it's a reference, when used in an expression it's the address-of
operator.

Let me start by asking a question to the C++ language lawyers out there:
what should this print out:
Compilation error?
--
Regards,
Steve

"...which means he created the heaven and the earth... in the DARK! How good
is that?"

Sep 29 '07 #5
On 2007-09-29 16:32, Summercool wrote:
Can we confirm the following? also someone said, Java also has
"reference" like in C++, which is an "implicit pointer":
Java have references, yes. But they are not the same thing as C++
references, neither when it comes to implementation or semantics.
Pointer and Reference
---------------------

I am starting to see what pointer and reference are and how they
relate
to each other.

in the C era, a pointer *is* a reference. that's why when we have
No, a pointer is a pointer. They can be used to implement reference
semantics (as opposed to value semantics).
int a = 10;
int *pi = &a;

and you can "dereference it":

*pi = 20;

Until when C++ comes along, then we have a new "reference":

int a = 10;
int i =& a; // or int i = &a; i am not sure about the syntax.
i = 20; // now both a and i are 20

so this type of reference is an implicit pointer... it points to a,
No, think about C++ references as aliases for the type they refer to.
One big difference between references and pointers is that references do
not have an address, while pointers do. Consider the following small
program:

int main() {
int a = 5;
int& b = a;
int* c = &a;
}

In a totally un-optimised program there is no guarantee that any more
memory than sizeof(int) + sizeof(int*) is allocated for variables in main().
but
you don't use the way in C (int *pi = &a) And when you use (i = 20),
it does the dereference silently. (*pi = 20;)

so a reference is new: a pointer but "looks like not a pointer".
One important difference between a pointer and a reference (in C++) is
that a pointer can be made to point at any object of the right type,
while a reference always points to only one object. In other words a
reference can not be reseated.

This is one of the bigger differences between C++ and Java references,
Java references can be reseated and are in that way more like a C/C++
pointer. However notice that a Java reference is not a pointer, it can
not be used to perform pointer arithmetic operations among other
differences.
come to think about it, in Java and Ruby, they are like that too.

a = Car.new

a doesn't look like a pointer, but it is actually a pointer.

we don't dereference it to get to the attributes like (*a).value = 10
or a->value = 10 but just use a.value = 10
That probably means that it is a reference, and not a pointer.
So from this point on, a reference and a pointer are not the same... a
reference is a pointer "that doesn't look like a pointer."

they both points to something. but the syntax (or grammar) of usage
doesn't look like it is a pointer in the C era. a reference is an
"automatically dereferenced" pointer, shall we say? or an "implicit"
pointer, or "silent" pointer.
I prefer to call it a reference.
[snip PHP which is off topic in both c.l.c++ and c.l.j.p]
In language we use nowadays, which language has "reference"
I would suspect that any language with OO support provides some kind of
reference, and probably a few others to.
In think C++, Java, (Perl?), Python, PHP5, Ruby all have reference.

In language we use nowadays, which language has "reference" to a
"reference"?

I think C++, Java, PHP5 do... not sure about Python and Ruby.
C++ does not have references to references, consider the following code:

int a;
int& b = a; // b is a reference to a
int& c = b; // c is a reference to a *not* b

C++ do, however, have pointers to pointers, or references to pointers,
but as previously pointed out pointers and references are entirely
separate things.

--
Erik Wikström
Sep 29 '07 #6
On 2007-09-29 17:09, Joshua Cranmer wrote:
Summercool wrote:
>Can we confirm the following? also someone said, Java also has
"reference" like in C++, which is an "implicit pointer":

Pointer and Reference
---------------------

I am starting to see what pointer and reference are and how they
relate
to each other.

in the C era, a pointer *is* a reference. that's why when we have

int a = 10;
int *pi = &a;

and you can "dereference it":

*pi = 20;

Until when C++ comes along, then we have a new "reference":

int a = 10;
int i =& a; // or int i = &a; i am not sure about the syntax.

int i = &a; is the syntax. Yes, the ampersand is used for both reference
and address-of, so it can be confusing (especially since I think that i
would be set to the address of a in C).
Actually they are both wrong, but you are party right that the & is the
address-of operator. The above can be written either as

int* i = &a; // i is a pointer

or

int& i = a; // i is a reference

Notice that the & when used for a reference is part of the type (just
like the * for a pointer).
>i = 20; // now both a and i are 20

so this type of reference is an implicit pointer... it points to a,

I wouldn't call it an implicit pointer: you cannot, to my knowledge,
change i to point to any other object but a like you can with a pointer.
Rather, i is an alias of a: the two objects forever more point to the
same thing.

>so a reference is new: a pointer but "looks like not a pointer".

Not quite. See above.
>come to think about it, in Java and Ruby, they are like that too.

No, it is not. In syntax, Java more nearly follows the reference syntax
but it is closer to a pointer with transparent {de}referencing.
>In language we use nowadays, which language has "reference"

In think C++, Java, (Perl?), Python, PHP5, Ruby all have reference.

In language we use nowadays, which language has "reference" to a
"reference"?

I think C++, Java, PHP5 do... not sure about Python and Ruby.

Let me start by asking a question to the C++ language lawyers out there:
what should this print out:

int a = 50, b = 20;
int i = &a;
Assuming you meant i to be a reference here (int& i = a;).
std::cout << "a: " << a << " b: " << b << " i: " << i;
a: 50 b: 20 i: 50
i = 30;
std::cout << "a: " << a << " b: " << b << " i: " << i;
a: 30 b: 20 i: 30
i = &b;
i = b; // Cannot reseat a reference
std::cout << "a: " << a << " b: " << b << " i: " << i;
a: 20 b: 20 i: 20
b = &a;
b = i;
std::cout << "a: " << a << " b: " << b << " i: " << i;
Same as above
a = &i; // Creating a circular loop?
a = i; // Same as "a = a;"
std::cout << "a: " << a << " b: " << b << " i: " << i;
Same as above

--
Erik Wikström
Sep 29 '07 #7
Rolf Magnus wrote:
>int i =& a; // or int i = &a; i am not sure about the syntax.

It doesn't matter. Whitespace before and after the & operator is ignored.
Oops. Even though my answer is right, this is - as others have pointed out -
not the correct syntax for defining a reference.

Sep 29 '07 #8
On 2007-09-29 17:32, Stefan Ram wrote:
Summercool <Su************@gmail.comwrites:
>>in the C era, a pointer *is* a reference. that's why when we have

In Java, reference values are pointers.
All references are pointers, but none are the kind of pointers usually
meant when discussing C or C++. For example in C and C++ if you have a
pointer p and do "p++;" that is an operation on the pointer. Whereas in
Java, if you have a reference r and do "r++;" that would be an operation
on the object r refers to (provided that Java supports operator
overloading, I can not remember if it does).

--
Erik Wikström
Sep 29 '07 #9
Erik Wikström wrote:
Assuming you meant i to be a reference here (int& i = a;).
I did.
i = b; // Cannot reseat a reference
And that was what I was most interested about. It was also what I suspected.
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
Sep 29 '07 #10
On Sep 29, 8:42 am, Erik Wikström <Erik-wikst...@telia.comwrote:
>
All references are pointers, but none are the kind of pointers usually
meant when discussing C or C++. For example in C and C++ if you have a
pointer p and do "p++;" that is an operation on the pointer. Whereas in
Java, if you have a reference r and do "r++;" that would be an operation
on the object r refers to [...]
So can we think of a C++ reference as:

Same as a pointer, 4 bytes.
Same as a pointer, points to some where.

Main difference: with pointer, you can print out the pointer, set the
pointer to different values, including 0. With reference, you can't.
You always dereference a reference when you use it.

Here are some C++ and C code equivalent:

int a = 10; // In C: the same
int avg = 20; // In C: the same

int& b = a; // In C: int *pa = &a;
printf "%d", b; // In C: printf "%d", *pa;
b = 20; // In C: *pa = 20;
// can't do // In C: pa = (int *) 0; or (int *) NULL;
// can't do // In C: pa = &avg;

int& c = b; // In C: int *pa2 = &(*pa)
// &(*pa) is &(20) which is illegal
// but if it is C++, it magically
// changes &(*pa) to just pa
// So in C, int *pa2 = pa;
So In Java, Python, PHP5, and Ruby, when you use

a = Dog.new

it is really not a reference, not a pointer, but
something in between.

Not a reference, because you can set where it points to:

a = nil

Not a pointer, because you use

a.bark()

instead of a->bark() or (*a).bark()

Sep 29 '07 #11
Summercool wrote:
On Sep 29, 8:42 am, Erik Wikström <Erik-wikst...@telia.comwrote:
>All references are pointers, but none are the kind of pointers usually
meant when discussing C or C++. For example in C and C++ if you have a
pointer p and do "p++;" that is an operation on the pointer. Whereas in
Java, if you have a reference r and do "r++;" that would be an operation
on the object r refers to [...]

So can we think of a C++ reference as:

Same as a pointer, 4 bytes.
Same as a pointer, points to some where.
Or the best way I have heard a reference describe is that you are
specifying another name to the same variable. The term reference is,
IMO, poorly named: a better name would be alias.
So In Java, Python, PHP5, and Ruby, when you use

a = Dog.new
I presume this is Ruby syntax? It is definitely neither Java nor PHP5
and I do not think that it is Python either. Considering that your
thread is X-posted to c.l.j.p and c.l.c++, you might want to compare C++
and Java syntax. Just a suggestion, though.
it is really not a reference, not a pointer, but
something in between.
In Java, it really is a pointer. The JLS actually specifically says that
reference values are pointers, as Stefan pointed out.
Not a pointer, because you use

a.bark()

instead of a->bark() or (*a).bark()
Who says that people have to use C-syntax for method dispatches? Since
the actual value of the pointer cannot be extracted, why put people
through syntactic hoops just to call a method? It *is* a pointer, *by
definition.*

I can show you another syntax that operates on pointers:

mov [eax], 5

Wow, I said "move five to the value of eax" without writing
*eax = 5;

Does that mean that eax is not a pointer but something similar to one?
P.S. I somewhat apologize for the sarcasm, but your posts do have a
characteristic of XXX is the right way and therefore everything else is
wrong.

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
Sep 29 '07 #12
On Sep 29, 9:57 am, Joshua Cranmer <Pidgeo...@verizon.netwrote:
Not a pointer, because you use
a.bark()
instead of a->bark() or (*a).bark()

Who says that people have to use C-syntax for method dispatches? Since
the actual value of the pointer cannot be extracted, why put people
through syntactic hoops just to call a method? It *is* a pointer, *by
definition.*
Now... so in Java, Python, PHP5, and Ruby, it looks like they are all
pointers when you say

a = new Dog("woofy")

or

a = Dog.new("woofy")

so they are all pointers, not reference. (because reference cannot
point to a different thing after it is set, like a = new Dog("lulu")
or a = nil)

the syntax a.color or a.bark is just a simpler way of writing C or C
++'s "->"

so that's it? I tend to compare the "." and the "->" as I view the
relatively modern language having similar syntax or operators...
(didn't expect "." to mean "->" in another language)
Sep 29 '07 #13
On 2007-09-29 18:39, Summercool wrote:
On Sep 29, 8:42 am, Erik Wikström <Erik-wikst...@telia.comwrote:
>>
All references are pointers, but none are the kind of pointers usually
meant when discussing C or C++. For example in C and C++ if you have a
pointer p and do "p++;" that is an operation on the pointer. Whereas in
Java, if you have a reference r and do "r++;" that would be an operation
on the object r refers to [...]

So can we think of a C++ reference as:

Same as a pointer, 4 bytes.
Same as a pointer, points to some where.
No, a pointer will always have an address, which you can print out or
whatever. Since it has an address it will also take up some memory
(either on the stack or on the heap). A reference does not have an
address and does not require memory to be allocated.

Another issue is how the compiler implements the semantics of a
reference, in some cases it will require allocating memory, but in some
cases it will not. For a pointer it will always allocate memory since
the standard requires that it does so. (All the above modulo any
optimisations).
Main difference: with pointer, you can print out the pointer, set the
pointer to different values, including 0. With reference, you can't.
You always dereference a reference when you use it.
The best way to understand the difference between a pointer and a
reference is to not try to compare them. They are two completely
separate concepts.
Here are some C++ and C code equivalent:

int a = 10; // In C: the same
int avg = 20; // In C: the same

int& b = a; // In C: int *pa = &a;
printf "%d", b; // In C: printf "%d", *pa;
b = 20; // In C: *pa = 20;
// can't do // In C: pa = (int *) 0; or (int *) NULL;
// can't do // In C: pa = &avg;

int& c = b; // In C: int *pa2 = &(*pa)
// &(*pa) is &(20) which is illegal
No, &(*pa) == pa, or put another way, you first dereferences the
pointer, and then takes the address of the integer returned. The address
of the int is then a int* pointing to the same place as pa.

--
Erik Wikström
Sep 29 '07 #14
On 2007-09-29 18:57, Joshua Cranmer wrote:
Summercool wrote:
>On Sep 29, 8:42 am, Erik Wikström <Erik-wikst...@telia.comwrote:
>>All references are pointers, but none are the kind of pointers usually
meant when discussing C or C++. For example in C and C++ if you have a
pointer p and do "p++;" that is an operation on the pointer. Whereas in
Java, if you have a reference r and do "r++;" that would be an operation
on the object r refers to [...]

So can we think of a C++ reference as:

Same as a pointer, 4 bytes.
Same as a pointer, points to some where.

Or the best way I have heard a reference describe is that you are
specifying another name to the same variable. The term reference is,
IMO, poorly named: a better name would be alias.
>So In Java, Python, PHP5, and Ruby, when you use

a = Dog.new

I presume this is Ruby syntax? It is definitely neither Java nor PHP5
and I do not think that it is Python either. Considering that your
thread is X-posted to c.l.j.p and c.l.c++, you might want to compare C++
and Java syntax. Just a suggestion, though.
>it is really not a reference, not a pointer, but
something in between.

In Java, it really is a pointer. The JLS actually specifically says that
reference values are pointers, as Stefan pointed out.
Just to clarify: it is a pointer, but not a C/C++ pointer. The closest
thing you get a Java reference in C++ is probably a struct wrapping a
pointer to the object and overloading the . operator so that it performs
a null-pointer check.

--
Erik Wikström
Sep 29 '07 #15
On Sep 29, 10:50 am, Erik Wikström <Erik-wikst...@telia.comwrote:
The best way to understand the difference between a pointer and a
reference is to not try to compare them. They are two completely
separate concepts.
really... just think of reference as an "alias"? the reason i will to
dig into reference is that when you pass a variable to a function, the
function can take it as a reference (in PHP5, but i am not sure if you
can do that in C++, but passing a to a function and have "a" modified
when the function returns? yuck!). And the function can return a
reference too. So in those cases, I kind of need to think of
reference as a pointer rather than an alias to have it make sense.


Sep 29 '07 #16
On Sep 29, 11:02 am, Summercool <Summercooln...@gmail.comwrote:
On Sep 29, 10:50 am, Erik Wikström <Erik-wikst...@telia.comwrote:
The best way to understand the difference between a pointer and a
reference is to not try to compare them. They are two completely
separate concepts.
here is what C++ in a Nutshell says for reference:

2.6.2.5 References

A reference is a synonym for an object or function. A reference is
declared just like a pointer, but with an ampersand (&) instead of an
asterisk (*). A local or global reference declaration must have an
initializer that specifies the target of the reference. Data members
and function parameters, however, do not have initializers. You cannot
declare a reference of a reference, a reference to a class member, a
pointer to a reference, an array of references, or a cv-qualified
reference. [...]

A reference, unlike a pointer, cannot be made to refer to a different
object at runtime. Assignments to a reference are just like
assignments to the referenced object.
Sep 29 '07 #17
On 2007-09-29 19:38, Summercool wrote:
On Sep 29, 9:57 am, Joshua Cranmer <Pidgeo...@verizon.netwrote:
Not a pointer, because you use
a.bark()
instead of a->bark() or (*a).bark()

Who says that people have to use C-syntax for method dispatches? Since
the actual value of the pointer cannot be extracted, why put people
through syntactic hoops just to call a method? It *is* a pointer, *by
definition.*

Now... so in Java, Python, PHP5, and Ruby, it looks like they are all
pointers when you say

a = new Dog("woofy")

or

a = Dog.new("woofy")

so they are all pointers, not reference. (because reference cannot
point to a different thing after it is set, like a = new Dog("lulu")
or a = nil)
I do not know, but at least in Java they are references, and I think
that they use the same terminology in the other languages you mentioned.
the syntax a.color or a.bark is just a simpler way of writing C or C
++'s "->"
Not necessarily, that depends on the semantics used in the language in
question. Just a guess but I would suspect that there are subtle
differences between references in all those languages.
so that's it? I tend to compare the "." and the "->" as I view the
relatively modern language having similar syntax or operators...
(didn't expect "." to mean "->" in another language)
Please try to separate different languages. Just because some concepts
have the same name in more than one language and share some
commonalities does not mean that they are the same thing or that they
can be easily explained in terms of some other language (unless you go
down to assembly level.

Just realise that a C/C++ pointer is not a C++ reference is not a Java
reference is not a C# reference, and so on. A Java reference is a Java
reference, nothing more nothing less, just like a C++ reference is a C++
reference, not a pointer or something else, it is just a reference.
--
Erik Wikström
Sep 29 '07 #18
On 2007-09-29 20:06, Summercool wrote:
On Sep 29, 11:02 am, Summercool <Summercooln...@gmail.comwrote:
>On Sep 29, 10:50 am, Erik Wikström <Erik-wikst...@telia.comwrote:
The best way to understand the difference between a pointer and a
reference is to not try to compare them. They are two completely
separate concepts.

here is what C++ in a Nutshell says for reference:

2.6.2.5 References

A reference is a synonym for an object or function. A reference is
declared just like a pointer, but with an ampersand (&) instead of an
asterisk (*). A local or global reference declaration must have an
initializer that specifies the target of the reference. Data members
and function parameters, however, do not have initializers. You cannot
declare a reference of a reference, a reference to a class member
The last part is not quite right, consider this:

struct Foo {
int i;
};

int main() {
Foo f;
int& ir = f.i; // Reference to class member
}

--
Erik Wikström
Sep 29 '07 #19
Summercool wrote:
Can we confirm the following? also someone said, Java also has
"reference" like in C++, which is an "implicit pointer":
Java references are like C++ pointers, with a few exceptions:

1. Java does not have pointer arithmetic.
2. Java cannot take the address of an existing object; pointers are created
only via the "new" operation.

In fact, Java pointers are almost exactly the same as Pascal pointers.

But Java references are nothing like C++ references, which

1. Cannot be reset.
2. Cannot be null.

This is all clearer if you realize that "." is Java is the same as "->" is
C++.
Sep 29 '07 #20
>Can we confirm the following? also someone said, Java also has
>"reference" like in C++, which is an "implicit pointer":
see http://mindprod.com/jgloss/pointer.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Sep 29 '07 #21
On Sat, 29 Sep 2007 16:57:22 GMT, Joshua Cranmer
<Pi*******@verizon.netwrote, quoted or indirectly quoted someone who
said :
>In Java, it really is a pointer. The JLS actually specifically says that
reference values are pointers, as Stefan pointed out.
In Java I believe the official definition of pointer is "a non-null
reference". However, nearly every Java programmer uses the term to
mean a low level machine pointer like C++ uses with manual referencing
and almost no restrictions on what you can do with it -- e.g. assign
it numeric values, do arithmetic, compose it with bit operators etc.

References are pointers with airbags.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Sep 29 '07 #22
On Sat, 29 Sep 2007 17:38:58 -0000, Summercool
<Su************@gmail.comwrote, quoted or indirectly quoted someone
who said :
>so they are all pointers, not reference. (because reference cannot
point to a different thing after it is set, like a = new Dog("lulu")
or a = nil)
In Java a final reference cannot be changed. An non-final reference
can.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Sep 29 '07 #23
On Sat, 29 Sep 2007 17:50:41 GMT, Erik Wikström
<Er***********@telia.comwrote, quoted or indirectly quoted someone
who said :
>The best way to understand the difference between a pointer and a
reference is to not try to compare them.
The best I think you could get is a difference within each language,
Java/C++ etc of the difference.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Sep 29 '07 #24
On 2007-09-29 20:30, Mike Schilling wrote:
Summercool wrote:
>Can we confirm the following? also someone said, Java also has
"reference" like in C++, which is an "implicit pointer":

Java references are like C++ pointers, with a few exceptions:

1. Java does not have pointer arithmetic.
2. Java cannot take the address of an existing object; pointers are created
only via the "new" operation.
It was some time since I last used Java, but would not something like
this work?

Foo f1 = new Foo();
Foo f2 = f1;

While it does not exactly take the address of the object referred to by
f1, it does create a new reference (pointer) that points to the same object.

--
Erik Wikström
Sep 29 '07 #25
Lew
Stefan Ram wrote:
<Er***********@telia.comwrites:
> Foo f1 = new Foo();
Foo f2 = f1;
While it does not exactly take the address of the object referred to by
f1, it does create a new reference (pointer) that points to the same object.

In Java terminology, it creates a new /variable/ f2 that
contains /the same/ reference value as the variable f1.

Many terms, like »variable« and »object«, have different
meanings in Java and C++, which are specifed in the JLS3 and
in ISO/IEC 14882, respectively.
For that matter, "address" doesn't have the meaning in Java that most people
seem to imagine. For one thing, it's not a number.

I interpret the metaJava [1] word "address" as "magical arrow pointing to the
thing".

[1] I coin "metaJava" to mean "the language in which one speaks about Java".
So this footnote must be metametaJava, "the language in which one speaks about
the language in which one speaks about Java".

--
Lew
Sep 30 '07 #26
On Sat, 29 Sep 2007 22:56:52 GMT, Erik Wikström
<Er***********@telia.comwrote, quoted or indirectly quoted someone
who said :
Foo f1 = new Foo();
Foo f2 = f1;

While it does not exactly take the address of the object referred to by
f1, it does create a new reference (pointer) that points to the same object.
In assembler, what that does in allocate space for a new Foo object on
the heap somewhere. It puts the 32-bit address of that object in f1, a
local variable allocated on the stack frame.

Then it copies the pointer value from f1 and stores it in f2.

No reference is created, except on the initial allocation.

References may also be implemented as handles rather than direct
pointers. See http://mindprod.com/jgloss/reference.html
http://mindprod.com/jgloss/pointer.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Sep 30 '07 #27
On Sat, 29 Sep 2007 20:23:25 -0400, Lew <le*@lewscanon.comwrote,
quoted or indirectly quoted someone who said :
>I interpret the metaJava [1] word "address" as "magical arrow pointing to the
thing".
The JVM leaves tremendous leeway in how addresses, pointers and
references are actually implemented. It is difficult to talk about
what Java does without something nailed down and concrete about what
MUST be happening at the machine code level.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Sep 30 '07 #28
Erik Wikström wrote:
On 2007-09-29 20:30, Mike Schilling wrote:
>Summercool wrote:
>>Can we confirm the following? also someone said, Java also has
"reference" like in C++, which is an "implicit pointer":

Java references are like C++ pointers, with a few exceptions:

1. Java does not have pointer arithmetic.
2. Java cannot take the address of an existing object; pointers are
created only via the "new" operation.

It was some time since I last used Java, but would not something like
this work?

Foo f1 = new Foo();
Foo f2 = f1;

While it does not exactly take the address of the object referred to
by
f1, it does create a new reference (pointer) that points to the same
object.
I would say (somewhat pedantically) that only the new operation "creates" a
pointer; your second line copies an existing pointer.
Sep 30 '07 #29
Summercool wrote:
Now... so in Java, Python, PHP5, and Ruby, it looks like they are all
pointers when you say

a = new Dog("woofy")

or

a = Dog.new("woofy")

so they are all pointers, not reference. (because reference cannot
point to a different thing after it is set, like a = new Dog("lulu")
or a = nil)
True, if you use C++ terminology.

False, if you use Java terminology.

In general I will recommend using the languages own terminology.

Arne
Sep 30 '07 #30
Roedy Green wrote:
On Sat, 29 Sep 2007 22:56:52 GMT, Erik Wikström
<Er***********@telia.comwrote, quoted or indirectly quoted someone
who said :
> Foo f1 = new Foo();
Foo f2 = f1;

While it does not exactly take the address of the object referred to by
f1, it does create a new reference (pointer) that points to the same object.

In assembler, what that does in allocate space for a new Foo object on
the heap somewhere. It puts the 32-bit address of that object in f1, a
local variable allocated on the stack frame.

Then it copies the pointer value from f1 and stores it in f2.
N bit address - 32 bit on some platforms.

Arne
Sep 30 '07 #31
Lew
Roedy Green wrote:
>[In Java,] It puts the 32-bit address of that object in ... a
local variable allocated on the stack frame.
Arne Vajhøj wrote:
N bit address - 32 bit on some platforms.
N.b., I know I've said it before, but in Java any particular N-bit pattern
associated with the object as an address can change during the lifetime of the
object, and in fact can be optimized away entirely during runtime. An address
in Java is a GUID for an object, but it doesn't perform like a number as C/C++
programmers are wont to understand addresses. The mental model of an
"address" is very different between Java and C/C++.

--
Lew
Sep 30 '07 #32
Lew wrote:
Roedy Green wrote:
>>[In Java,] It puts the 32-bit address of that object in ... a
local variable allocated on the stack frame.

Arne Vajhøj wrote:
>N bit address - 32 bit on some platforms.

N.b., I know I've said it before, but in Java any particular N-bit
pattern associated with the object as an address can change during
the lifetime of the object, and in fact can be optimized away
entirely during runtime. An address
in Java is a GUID for an object, but it doesn't perform like a number as
C/C++
programmers are wont to understand addresses. The mental model of an
"address" is very different between Java and C/C++.
The same is true in C++, though it's perhaps less obvious. There are
garbage collectors for C++, some of which compact as well as collect. It's
also possible for the value of a C++ pointer not to be a machine address,
even though that's by far the most common implementation.

If what you're saying is that most C++ programmers either don't know this or
program as if they don't, we're entirely in agreement.,
Oct 1 '07 #33
Lew wrote:
Roedy Green wrote:
>>[In Java,] It puts the 32-bit address of that object in ... a
local variable allocated on the stack frame.

Arne Vajhøj wrote:
>N bit address - 32 bit on some platforms.

N.b., I know I've said it before, but in Java any particular N-bit
pattern associated with the object as an address can change during the
lifetime of the object, and in fact can be optimized away entirely
during runtime. An address in Java is a GUID for an object, but it
doesn't perform like a number as C/C++ programmers are wont to
understand addresses. The mental model of an "address" is very
different between Java and C/C++.
It may change.

But eventually some code will use an address into memory.

My point was that the address is not necessary a 32 bit address.

Arne
Oct 1 '07 #34
On Sep 29, 5:42 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-09-29 17:32, Stefan Ram wrote:
Summercool <Summercooln...@gmail.comwrites:
>in the C era, a pointer *is* a reference. that's why when we have
In Java, reference values are pointers.
All references are pointers, but none are the kind of pointers usually
meant when discussing C or C++. For example in C and C++ if you have a
pointer p and do "p++;" that is an operation on the pointer. Whereas in
Java, if you have a reference r and do "r++;" that would be an operation
on the object r refers to (provided that Java supports operator
overloading, I can not remember if it does).
Alf really got it right when he said that it's just arbitrary
terminology, and that each language has its own definitions.
Never the less: the difference between pointers (called
references) in Java, and pointers in C++, is that in C++, a
pointer is, itself, an object in its own right. Unlike Java,
C++ doesn't divide its types into two incompatible categories;
an object can have any type you want, any type may support copy,
operators, etc., and you can take the address of any type. And
pointers are just another object type, with no particular
special rules. All of the rest (pointers to pointers, etc.)
follows.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 1 '07 #35
On Sep 29, 6:39 pm, Summercool <Summercooln...@gmail.comwrote:
On Sep 29, 8:42 am, Erik Wikström <Erik-wikst...@telia.comwrote:
All references are pointers, but none are the kind of pointers usually
meant when discussing C or C++. For example in C and C++ if you have a
pointer p and do "p++;" that is an operation on the pointer. Whereas in
Java, if you have a reference r and do "r++;" that would be an operation
on the object r refers to [...]
So can we think of a C++ reference as:
Same as a pointer, 4 bytes.
No. The standard is very explicit; a reference doesn't have to
occupy any memory at all. (And the size of a pointer isn't
necessarily 4 bytes. For that matter, it isn't necessarily the
same for all pointer types.)

Since a reference isn't an object, and you can never obtain its
address, it's really irrelevant.
Same as a pointer, points to some where.
It designates an object. How it does so is up to the
implementation.
Main difference: with pointer, you can print out the pointer,
set the pointer to different values, including 0. With
reference, you can't. You always dereference a reference when
you use it.
That's a frequent way of thinking about it, and it may help
understanding. But formally, a reference is simply an lvalue
which refers to an object.
Here are some C++ and C code equivalent:
int a = 10; // In C: the same
int avg = 20; // In C: the same
int& b = a; // In C: int *pa = &a;
printf "%d", b; // In C: printf "%d", *pa;
b = 20; // In C: *pa = 20;
// can't do // In C: pa = (int *) 0; or (int *) NULL;
// can't do // In C: pa = &avg;
int& c = b; // In C: int *pa2 = &(*pa)
// &(*pa) is &(20) which is illegal
// but if it is C++, it magically
// changes &(*pa) to just pa
// So in C, int *pa2 = pa;
So In Java, Python, PHP5, and Ruby, when you use
a = Dog.new
it is really not a reference, not a pointer, but
something in between.
In C++ terminology, it's a pointer. The only difference is that
in Java (and I presume, the other languages as well), pointers
aren't a recognized object type, so you cannot to things with
them that you could do with an object type. (In Java, at least,
this is also the case for the basic types, such as int or
double.)
Not a reference, because you can set where it points to:
a = nil
Not a pointer, because you use
a.bark()
instead of a->bark() or (*a).bark()
That's just syntax.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 1 '07 #36
On Sep 30, 12:46 am, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
On Sat, 29 Sep 2007 16:57:22 GMT, Joshua Cranmer
<Pidgeo...@verizon.netwrote, quoted or indirectly quoted someone who
said :
In Java, it really is a pointer. The JLS actually
specifically says that reference values are pointers, as
Stefan pointed out.
In Java I believe the official definition of pointer is "a non-null
reference". However, nearly every Java programmer uses the term to
mean a low level machine pointer like C++ uses with manual referencing
and almost no restrictions on what you can do with it -- e.g. assign
it numeric values, do arithmetic, compose it with bit operators etc.
That's the terminology in a specific community. Each community
developes its own terminology. (I've heard people say that Java
references aren't pointers because Java doesn't support pointer
arithmetic. But of course, saying that the difference between
a pointer and a reference is that pointers support arithmetic is
pretty arbitrary.)
References are pointers with airbags.
References in Java, you mean:-). (But I like the expression.)

In C++, references and pointers are two very different things.
So the C++ community gives them different names, and insists on
the difference, the fact that the names name different things.
In Java (and most other languages), you don't really have two
separate concepts here, so in a very real sense, references and
pointers can be considered two different names for more or less
the same thing. Distinguishing the two doesn't necessarily add
any clarity or help the understanding of Java in any real way,
where as it's essential for C++. If I had to characterize
Java's references in terms of C++, I'd say that they were
pointers, but then point out that 1) pointers (like int and
double) aren't "objects" in Java: a pointer doesn't have an
address, you can't new it, etc., and 2) Java doesn't support
pointer arithmetic (just as it doesn't support implicit
conversion of double to int).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 1 '07 #37
James Kanze wrote:
On Sep 29, 5:42 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
>On 2007-09-29 17:32, Stefan Ram wrote:
Summercool <Summercooln...@gmail.comwrites:
in the C era, a pointer *is* a reference. that's why when we have
In Java, reference values are pointers.
>All references are pointers, but none are the kind of pointers usually
meant when discussing C or C++. For example in C and C++ if you have a
pointer p and do "p++;" that is an operation on the pointer. Whereas in
Java, if you have a reference r and do "r++;" that would be an operation
on the object r refers to (provided that Java supports operator
overloading, I can not remember if it does).

Alf really got it right when he said that it's just arbitrary
terminology, and that each language has its own definitions.
Never the less: the difference between pointers (called
references) in Java, and pointers in C++, is that in C++, a
pointer is, itself, an object in its own right. Unlike Java,
C++ doesn't divide its types into two incompatible categories;
an object can have any type you want, any type may support copy,
operators, etc., and you can take the address of any type.
Just a nit: I think you should say that C++ does not devide its _objects_
into incompatible categories. The type system, however, does not look at
all that homogeneous. It strikes me that there are at least three different
categories of types:

a) function signatures
b) object types (built-in types, pointers, classes, enums, maybe more)
c) reference types

It is true that you cannot have objects of types in the classes a) and c).
However, the template mechanism recognizes those as distinct types of their
own, e.g., the type

bool(int)

is different from

bool(*)(int)
And
pointers are just another object type, with no particular
special rules. All of the rest (pointers to pointers, etc.)
follows.

Best

Kai-Uwe Bux
Oct 1 '07 #38
On 2007-10-01 13:32, Stefan Ram wrote:
James Kanze <ja*********@gmail.comwrites:
>>in C++, a pointer is, itself, an object in its own right.

In C++,

»An object is a region of storage. (...)«

ISO/IEC 14882:2003(E), 1.8

A pointer is a value of a pointer type, like »&c« or »( int * )0«.

A pointer, therefore, does not need to be an object.
A pointer also have an address (i.e. occupy some region of storage).

--
Erik Wikström
Oct 1 '07 #39
On 2007-09-30 23:40:49 -1000, James Kanze <ja*********@gmail.comsaid:
Never the less: the difference between pointers (called
references) in Java,
Almost always called references. When you dereference a null reference
in Java, the runtime throws a NullPointerException object. <g>

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Oct 1 '07 #40
Pete Becker wrote:
On 2007-09-30 23:40:49 -1000, James Kanze <ja*********@gmail.com>
said:
>Never the less: the difference between pointers (called
references) in Java,

Almost always called references. When you dereference a null reference
in Java, the runtime throws a NullPointerException object. <g>
For fun, I grepped the JLS for "pointer". Almost all of the hits are the
word NullPointerException, though there's also the definition of "reference"

The reference values (often just references) are pointers to these
objects,
and a special null reference, which refers to no object.

and a few slipups where "reference" was meant.
Oct 1 '07 #41
On Sep 30, 2:32 am, Summercool <Summercooln...@gmail.comwrote:
Until when C++ comes along, then we have a new "reference":

int a = 10;
int i =& a; // or int i = &a; i am not sure about the syntax.
i = 20; // now both a and i are 20
so this type of reference is an implicit pointer... it points to a,
but
you don't use the way in C (int *pi = &a) And when you use (i = 20),
it does the dereference silently. (*pi = 20;)
It is confusing to think of them this way. I like
to call them "aliases" to avoid the confusion
with the computer science term "reference" , which
can be applied to pointers.

Imagine a cubby-hole in your computer's memory
with the value 20 in it. Above the hole are the
labels "a" and "i".

The labels "a" and "i" both equally refer to the
same memory location. These two code snippets
have exactly identical effects:

int i = 20;
int &a = i;

and

int a = 20;
int &i = a;
Oct 2 '07 #42

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
9
by: Sandy | last post by:
Hi, In one of my interview I was asked a question, whether using pointers for argument is efficient then reference or not. i.e. void fun(Complex *p) void fun(Complex &ref) can somebody...
18
by: man | last post by:
can any one please tell me what is the diff between pointer and reference.....and which one is better to use ....and why???????
12
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
13
by: al.cpwn | last post by:
I get that these two are different int* get() { static int m; return &m; } int& get() {
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
8
by: toton | last post by:
HI, One more small doubt from today's mail. I have certain function which returns a pointer (sometimes a const pointer from a const member function). And certain member function needs reference...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
2
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.