Pointer vs Reference

S

Summercool

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.
 
R

Rolf Magnus

Summercool said:
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).
 
J

Joshua Cranmer

Summercool said:
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.
 
A

Alf P. Steinbach

[Note to others: crossposted to [comp.lang.c++] and
[comp.lang.java.programmer]].

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

"pointer", "reference", it's just terminology.

What these words mean depends on the programming language.

The special meaning of "reference" in C++ is unfortunate but a fact that
we must live with, it makes it very difficult to discuss these things in
general. Java does not have anything like it.

If you compare only the built-in types, a Java variable of class type, a
Java reference,

SomeType o = new SomeType(); // Java reference

(hope I got the syntax right, it's a long time since I did Java, and I'm
too lazy to put the examples to compilers!)

corresponds most closely to a C++ pointer,

SomeType* p = new SomeType(); // C++ pointer

but there is a huge difference in what you can do, apart from the syntax
issues: C++ supports pointers to pointers, which means that you can
write e.g. a function swap that exchanges the values of two pointer
variables (or any variables), while in Java that level of indirection is
only available for array elements, and then with run-time type checking.

Also, the Java reference provides guaranteed automatic garbage
collection (freeing memory when the object is no longer used), while in
C++ that's not guaranteed, and is in fact not common -- the language
doesn't prevent automatic garbage collection, but doesn't currently
support it either: automatic garbage collection for C++ exists, but is
more like experimental.

The C++ concept corresponding most closely to a Java reference is
therefore a smart pointer, an object that encapsulates a pointer and
provides garbage collection (not general automatic garbage collection
but reference-counted automatic garbage collection, which differs in
that it doesn't automatically deal with cycles of objects),

shared_ptr<SomeType> p( new SomeType() );

but that's not built-in, it's a library solution.

Cheers, & hth.,

- Alf
 
S

Steve Folly

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?"
 
G

Guest

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.
 
G

Guest

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 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.



Not quite. See above.


No, it is not. In syntax, Java more nearly follows the reference syntax
but it is closer to a pointer with transparent {de}referencing.


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; // Cannot reseat a reference
std::cout << "a: " << a << " b: " << b << " i: " << i;

a: 20 b: 20 i: 20

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
 
R

Rolf Magnus

Rolf said:
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.
 
G

Guest

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).
 
S

Summercool

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()
 
J

Joshua Cranmer

Summercool said:
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.
 
S

Summercool

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)
 
G

Guest

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.
 
G

Guest

Summercool said:
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.
 
S

Summercool

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.
 
S

Stefan Ram

=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?= said:
Just to clarify: it is a pointer, but not a C/C++ pointer.

In C++, a reference is the same as any other variable, /after/
it has been created. The difference is in the act of creation.

For example, in C++, in

int main()
{ int v( 12 );
int & w( v );
/* ... */ }

, v is a »variable«, and »w« is a reference, but at the part
marked »/* ... */«, there is nothing than can tell v from w anymore.
Both behave /exactly/ the same - the only difference is the name.

If have written this down in some more detail, but in German language:

http://www.purl.org/stefan_ram/pub/c++_referenzdefinition_de
 
S

Summercool

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top