Skip to main content

Pointers and References - oh no!

Submitted by mcdrewski on
Forum

I've taken the [url="http://www.sumea.com.au/forum/topic.asp?TOPIC_ID=3304"]side discussion on references and pointers from lorien's post here[/url] off to it's own tread, since I actually feel strongly about it.

The thread so far:
quote:
rezn0r
Java doesn't have pointers. :P

quote:
mcdrewski
I know this was a joke, but every damn procedural language has pointers, they just call them references, aliases or whatever.

quote:
Dragoon
Actually no they aren't the same.
What's a null reference? What's a null pointer?
That's right the first doesn't exist.

quote:
mcdrewski
In C++, Dragoon is right, the term 'null reference' has no meaning. However there absolutely is a concept which can be described as 'null references'. For example, in Java, think about the following chunk of code.

Player p[];
p = new Player[10];

Until each Player object in the array is instantiated, we have an array of references to uninstantiated Player objects. 'null references' for want of a better term.

In short, as soon as you have a way of describing one object by two different names, you have aliases, or references, or pointers etc. Dig deep enough in virtually any procedural language and you'll start to find traps,tricks and caveats that only make sense if you think in terms of pointers. The language might do everything in it's power to hide those tricks from you so you never try any form of *i++ malarky, but under the covers it's all the same thing (since at CPU level the computer can only think in terms of data or the address where that data is stored).

quote:
dragoon
Yes the underlying implementation of references would indeed use pointers at some time, however consider:

*p = 10;

Some languages may have specific situations where you might call them something similar to null references but they can't be used like a null pointer (comparison wise can you use them in logical operations? maybe you can? I would imagine in Java that in your array situation arr[0] != arr[1] even though they are not initialised - likely it would throw a not initialised exception or something as it can't get the hash code of the objects). NB: Java isn't my strongest language.

The concept of a pointer is also different from the concept of a reference. Pointers are a location in memory, which can be used as a reference, but allow you to do a whole lot more (and shoot yourself in the foot in a myriad of additional ways). A reference is just that, a reference to an object that allows the compiler or interpreter to match up the code using it to the original object.

I'll respond at lunchtime since it's time to go to work now :)

Submitted by lorien on Sat, 05/11/05 - 10:50 AM Permalink

Sorry mcdrewski, but I can't help pointing out the origin of the phrase that's got this rant started "graduates who don't understand the difference between a pointer to an array and an array of pointers" (or something like it).

I pinched it. The guilty party was actually Entr0py- one of my classmates who was having a bloody good rant about Certain People and the course we did at the time on that same blog of Yusuf's [:)]

It's offline now :(

Submitted by mcdrewski on Sat, 05/11/05 - 12:02 PM Permalink

Yeas indeedy. And while Dragoon is 100% right, a pointer is not identical to a reference, my point is kind of that all these languages have concepts which have the same mental gymnastics required as understanding pointers.

So back to your original phrase, it works just as well with references. "reference to an array and an array of references" is the same conceptual thing.

So I posit that to solve the problem we just say that a pointer is a special kind of reference and that they trip up n00bs. [:)]

Submitted by wicked on Tue, 08/11/05 - 9:13 AM Permalink

Even though it's not common knowledge, null references exist in C++. It's only a bit hard to make that mistake.
First time I saw it I was pretty amazed. How come this is null now? I'm in this method from a reference! Not possible!

Here's proof of the elusive bastard:
[code]
#include
#include

using namespace std;

int main()
{
string *p = NULL;
string &r = (string &) (*p);
cout << r.size() << "
";
}
[/code]

Submitted by lorien on Tue, 08/11/05 - 9:55 AM Permalink

quote:Originally posted by wicked

Even though it's not common knowledge, null references exist in C++. It's only a bit hard to make that mistake.
First time I saw it I was pretty amazed. How come this is null now? I'm in this method from a reference! Not possible!

Here's proof of the elusive bastard:
[code]
#include
#include

using namespace std;

int main()
{
string *p = NULL;
string &r = (string &) (*p);
cout << r.size() << "
";
}
[/code]

10 points for being guilty of that code :) I've been guilty of it too :(

Dragoon posted above about the crazy power of pointers, they are really dangerous, but they're just so cool because it's though pointers that you can do things that you'd otherwise have to do in asm.

Plain old K&R C was designed to be a kind of portable, structured assembly language for making unix in, and you do need that sort of power when developing an OS.

Submitted by matt davo on Thu, 08/12/05 - 8:17 AM Permalink

return this;

(That one is for lorien who will get a giggle)

Submitted by matt davo on Thu, 08/12/05 - 8:19 AM Permalink

actually this should get a laugh from lorien too...
and possibly the bullant team.

inline Vector3* Vector3::operator-()
{
return new Vector3(-x, -y, -z);
}

maybe... sorry, but you guys hired him.

Submitted by mcdrewski on Thu, 08/12/05 - 8:42 AM Permalink

hooray for garbage collected languages, I say [:)]

Submitted by redwyre on Thu, 08/12/05 - 11:15 PM Permalink

Don't forget that garbage collection is a valid memory model for C++...

Submitted by lorien on Fri, 09/12/05 - 6:23 AM Permalink

quote:Originally posted by matt.davo


actually this should get a laugh from lorien too...
and possibly the bullant team.

inline Vector3* Vector3::operator-()
{
return new Vector3(-x, -y, -z);
}

maybe... sorry, but you guys hired him.

Poor Bullant [:(] You guys must be a lot more careful about the AIE grads you hire after that one...

Posted by mcdrewski on
Forum

I've taken the [url="http://www.sumea.com.au/forum/topic.asp?TOPIC_ID=3304"]side discussion on references and pointers from lorien's post here[/url] off to it's own tread, since I actually feel strongly about it.

The thread so far:
quote:
rezn0r
Java doesn't have pointers. :P

quote:
mcdrewski
I know this was a joke, but every damn procedural language has pointers, they just call them references, aliases or whatever.

quote:
Dragoon
Actually no they aren't the same.
What's a null reference? What's a null pointer?
That's right the first doesn't exist.

quote:
mcdrewski
In C++, Dragoon is right, the term 'null reference' has no meaning. However there absolutely is a concept which can be described as 'null references'. For example, in Java, think about the following chunk of code.

Player p[];
p = new Player[10];

Until each Player object in the array is instantiated, we have an array of references to uninstantiated Player objects. 'null references' for want of a better term.

In short, as soon as you have a way of describing one object by two different names, you have aliases, or references, or pointers etc. Dig deep enough in virtually any procedural language and you'll start to find traps,tricks and caveats that only make sense if you think in terms of pointers. The language might do everything in it's power to hide those tricks from you so you never try any form of *i++ malarky, but under the covers it's all the same thing (since at CPU level the computer can only think in terms of data or the address where that data is stored).

quote:
dragoon
Yes the underlying implementation of references would indeed use pointers at some time, however consider:

*p = 10;

Some languages may have specific situations where you might call them something similar to null references but they can't be used like a null pointer (comparison wise can you use them in logical operations? maybe you can? I would imagine in Java that in your array situation arr[0] != arr[1] even though they are not initialised - likely it would throw a not initialised exception or something as it can't get the hash code of the objects). NB: Java isn't my strongest language.

The concept of a pointer is also different from the concept of a reference. Pointers are a location in memory, which can be used as a reference, but allow you to do a whole lot more (and shoot yourself in the foot in a myriad of additional ways). A reference is just that, a reference to an object that allows the compiler or interpreter to match up the code using it to the original object.

I'll respond at lunchtime since it's time to go to work now :)


Submitted by lorien on Sat, 05/11/05 - 10:50 AM Permalink

Sorry mcdrewski, but I can't help pointing out the origin of the phrase that's got this rant started "graduates who don't understand the difference between a pointer to an array and an array of pointers" (or something like it).

I pinched it. The guilty party was actually Entr0py- one of my classmates who was having a bloody good rant about Certain People and the course we did at the time on that same blog of Yusuf's [:)]

It's offline now :(

Submitted by mcdrewski on Sat, 05/11/05 - 12:02 PM Permalink

Yeas indeedy. And while Dragoon is 100% right, a pointer is not identical to a reference, my point is kind of that all these languages have concepts which have the same mental gymnastics required as understanding pointers.

So back to your original phrase, it works just as well with references. "reference to an array and an array of references" is the same conceptual thing.

So I posit that to solve the problem we just say that a pointer is a special kind of reference and that they trip up n00bs. [:)]

Submitted by wicked on Tue, 08/11/05 - 9:13 AM Permalink

Even though it's not common knowledge, null references exist in C++. It's only a bit hard to make that mistake.
First time I saw it I was pretty amazed. How come this is null now? I'm in this method from a reference! Not possible!

Here's proof of the elusive bastard:
[code]
#include
#include

using namespace std;

int main()
{
string *p = NULL;
string &r = (string &) (*p);
cout << r.size() << "
";
}
[/code]

Submitted by lorien on Tue, 08/11/05 - 9:55 AM Permalink

quote:Originally posted by wicked

Even though it's not common knowledge, null references exist in C++. It's only a bit hard to make that mistake.
First time I saw it I was pretty amazed. How come this is null now? I'm in this method from a reference! Not possible!

Here's proof of the elusive bastard:
[code]
#include
#include

using namespace std;

int main()
{
string *p = NULL;
string &r = (string &) (*p);
cout << r.size() << "
";
}
[/code]

10 points for being guilty of that code :) I've been guilty of it too :(

Dragoon posted above about the crazy power of pointers, they are really dangerous, but they're just so cool because it's though pointers that you can do things that you'd otherwise have to do in asm.

Plain old K&R C was designed to be a kind of portable, structured assembly language for making unix in, and you do need that sort of power when developing an OS.

Submitted by matt davo on Thu, 08/12/05 - 8:17 AM Permalink

return this;

(That one is for lorien who will get a giggle)

Submitted by matt davo on Thu, 08/12/05 - 8:19 AM Permalink

actually this should get a laugh from lorien too...
and possibly the bullant team.

inline Vector3* Vector3::operator-()
{
return new Vector3(-x, -y, -z);
}

maybe... sorry, but you guys hired him.

Submitted by mcdrewski on Thu, 08/12/05 - 8:42 AM Permalink

hooray for garbage collected languages, I say [:)]

Submitted by redwyre on Thu, 08/12/05 - 11:15 PM Permalink

Don't forget that garbage collection is a valid memory model for C++...

Submitted by lorien on Fri, 09/12/05 - 6:23 AM Permalink

quote:Originally posted by matt.davo


actually this should get a laugh from lorien too...
and possibly the bullant team.

inline Vector3* Vector3::operator-()
{
return new Vector3(-x, -y, -z);
}

maybe... sorry, but you guys hired him.

Poor Bullant [:(] You guys must be a lot more careful about the AIE grads you hire after that one...