Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Pointer vs Reference

Reply
Thread Tools

Pointer vs Reference

 
 
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
Posts: n/a
 
      09-29-2007
On 2007-09-29 17:32, Stefan Ram wrote:
> Summercool <(E-Mail Removed)> writes:
>>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
 
Reply With Quote
 
 
 
 
Joshua Cranmer
Guest
Posts: n/a
 
      09-29-2007
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
 
Reply With Quote
 
 
 
 
Summercool
Guest
Posts: n/a
 
      09-29-2007
On Sep 29, 8:42 am, Erik Wikstrm <(E-Mail Removed)> wrote:
>
> 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()



 
Reply With Quote
 
Joshua Cranmer
Guest
Posts: n/a
 
      09-29-2007
Summercool wrote:
> On Sep 29, 8:42 am, Erik Wikstrm <(E-Mail Removed)> wrote:
>> 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
 
Reply With Quote
 
Summercool
Guest
Posts: n/a
 
      09-29-2007
On Sep 29, 9:57 am, Joshua Cranmer <(E-Mail Removed)> wrote:

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


 
Reply With Quote
 
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
Posts: n/a
 
      09-29-2007
On 2007-09-29 18:39, Summercool wrote:
> On Sep 29, 8:42 am, Erik Wikström <(E-Mail Removed)> wrote:
>>
>> 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
 
Reply With Quote
 
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
Posts: n/a
 
      09-29-2007
On 2007-09-29 18:57, Joshua Cranmer wrote:
> Summercool wrote:
>> On Sep 29, 8:42 am, Erik Wikström <(E-Mail Removed)> wrote:
>>> 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
 
Reply With Quote
 
Summercool
Guest
Posts: n/a
 
      09-29-2007
On Sep 29, 10:50 am, Erik Wikstrm <(E-Mail Removed)> wrote:

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






 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      09-29-2007
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?= <(E-Mail Removed)> writes:
>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...zdefinition_de

 
Reply With Quote
 
Summercool
Guest
Posts: n/a
 
      09-29-2007
On Sep 29, 11:02 am, Summercool <(E-Mail Removed)> wrote:
> On Sep 29, 10:50 am, Erik Wikstrm <(E-Mail Removed)> wrote:
>
> > 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Reference counting and API (const reference vs pointer oriented) mathieu C++ 8 08-31-2008 09:05 AM
reference and pointer-reference kasthurirangan.balaji@gmail.com C++ 4 12-24-2007 09:05 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM
Passing the value by reference is same as pointer by reference sam pal C++ 3 07-16-2003 09:14 PM



Advertisments