Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411517 Posts in 69377 Topics- by 58431 Members - Latest Member: Bohdan_Zoshchenko

April 27, 2024, 11:35:38 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)[s]Get / Set in c++?[/s] pointless debate about references and pointers
Pages: 1 2 [3] 4
Print
Author Topic: [s]Get / Set in c++?[/s] pointless debate about references and pointers  (Read 10899 times)
Average Software
Level 10
*****

Fleeing all W'rkncacnter


View Profile WWW
« Reply #40 on: May 03, 2009, 01:33:17 PM »

I think you're splitting hairs. Binding a reference is just another name for the analogous operation of assigning something to a pointer. Semantically, it's identical. Sure, there are lots of constraints (binding can only and must be done when a reference is created, you can't change the object a reference is bound to, etc), but it's still a strict subset.

Your strict subset is argument is invalid.  Consider this...

References support assignment (they don't, but you claim they do, so let's assume they do).  Strings support assignments.  References support a strict subset of the operations on strings, so therefore references are strings.

This is the exact same logic you are using.  If you believe references are pointers based on your argument, you must also conclude that references are strings.

Please explain to me how binding a reference is semantically different from assigning to a pointer.

Gladly.

Code:
int i;
int *p = &i;  // Copies a value (&i) into p.
int &r = i; // Actions on r now affect i.
            // This might perform a pointer copy, like the previous line.
            // Many compilers will simply substitute i for r, since the referent of r is known to be i.
            // In that case, no copy of anything takes place (semantic difference).
            // r might not have run-time storage, whereas p will (semantic difference).

If that doesn't convince you, consider this:

A reference to const can be bound to a literal constant or to a temporary.  Literal constants have no memory addresses (string constants excluded), therefore pointers cannot refer to them.  There is no way to take the address of a temporary, therefore pointers cannot refer to them.

Code:
const int &r = 0; // Legal.
const int *p = &0; // Illegal.  0 does not have an address.

If it sounds like I'm splitting hairs, I am.  I study compilers and programming languages.  The reference/pointer argument drives me crazy because it was settled over a decade ago.  C++ references are completely unrelated to both C++ pointers and Java references.  This is not just my opinion, it is established fact.
Logged



What would John Carmack do?
Mikademus
Level 10
*****


The Magical Owl


View Profile
« Reply #41 on: May 03, 2009, 01:38:57 PM »

That's part of the constraints I was talking about. Everything you can do with a reference, you can do with a pointer, but not vice versa.

Nope. Using C++ semantics for illustration:

Pointer:

Code:
int my_int = 42;
int my_other_int = 84;
int *p_int = &my_int; // set target
p_int = &my_other_int; // reassign
*p_int = 42; // set target value
p_int = NULL; // assign NULL


References:

Code:
int &r_int; // COMPILER ERROR: references must be bound
int &r_int = my_int; // set target, BIND ONCE ONLY
r_int = my_other_int; // set target VALUE, reassign impossible
r_int = NULL; // set target VALUE (zero)
r_int = *((int *) NULL); // assignment to 0 illegal >> COMPILE ERROR

Bonus special:
int *p_int = 0;
r_int = *p_int; // good way to crash and burn >> COMPILE OR RUNTIME ERROR


The Java assignment operator is a good example of that Java "references" are really pointers; assignment does not set the value of the pointer target but reassigns the pointer to another target. This is probably the first gotcha that newbies encounter when learning the language.
« Last Edit: May 03, 2009, 01:46:40 PM by Mikademus » Logged

\\\"There\\\'s a tendency among the press to attribute the creation of a game to a single person,\\\" says Warren Spector, creator of Thief and Deus Ex. --IGN<br />My compilation of game engines for indies
Mikademus
Level 10
*****


The Magical Owl


View Profile
« Reply #42 on: May 03, 2009, 01:54:10 PM »

Code:
const int &r = 0; // Legal.
const int *p = &0; // Illegal.  0 does not have an address.

To actually split hairs, 0 does have an address on some platforms. I think PDP-10 .. PDP-20 (UNIX-based OS for VAX mainframes) allowed referencing address 0.  Coffee <-- we need a :cheers-mountaindew: or :cheers-jolt: emoticon
Logged

\\\"There\\\'s a tendency among the press to attribute the creation of a game to a single person,\\\" says Warren Spector, creator of Thief and Deus Ex. --IGN<br />My compilation of game engines for indies
Average Software
Level 10
*****

Fleeing all W'rkncacnter


View Profile WWW
« Reply #43 on: May 03, 2009, 01:57:49 PM »

Code:
const int &r = 0; // Legal.
const int *p = &0; // Illegal.  0 does not have an address.

To actually split hairs, 0 does have an address on some platforms. I think PDP-10 .. PDP-20 (UNIX-based OS for VAX mainframes) allowed referencing address 0.  Coffee <-- we need a :cheers-mountaindew: or :cheers-jolt: emoticon

Yeah, I know some platforms have a hardware 0, but that's not the concern of C++.  &0 is illegal even on those platforms, I believe.
Logged



What would John Carmack do?
Mikademus
Level 10
*****


The Magical Owl


View Profile
« Reply #44 on: May 03, 2009, 02:13:44 PM »

Code:
const int &r = 0; // Legal.
const int *p = &0; // Illegal.  0 does not have an address.

To actually split hairs, 0 does have an address on some platforms. I think PDP-10 .. PDP-20 (UNIX-based OS for VAX mainframes) allowed referencing address 0.  Coffee <-- we need a :cheers-mountaindew: or :cheers-jolt: emoticon

Yeah, I know some platforms have a hardware 0, but that's not the concern of C++.  &0 is illegal even on those platforms, I believe.

Quoting the "Vaxocentrism" entry of the 1991 ed. New Hacker's Dictionary (that I actually have in dead-tree version):
Quote
1. The assumption that dereferencing a NULL pointer is safe because it is all bits 0, and location 0 is readable and 0
Note that ESR speaks in a C-coding context, though. I had a quick look for if the Standard says something about dereferencing 0 in C++, but it seems to be intentionally undefined.
Logged

\\\"There\\\'s a tendency among the press to attribute the creation of a game to a single person,\\\" says Warren Spector, creator of Thief and Deus Ex. --IGN<br />My compilation of game engines for indies
BorisTheBrave
Level 10
*****


View Profile WWW
« Reply #45 on: May 03, 2009, 02:33:16 PM »

Ok, you've all angered me enough to waste my time on this.
C++ Pointer  C++ Reference  Java Reference 
Can ReassignYesNoYes
Can do pointer arithmeticYesNoNo
Can take null valuesYesNoYes
Can indirect by multiple levelsYesNoNo

They are all different things.
1) You cannot say two things are the same but one is constrained - clearly that is something different about them making them not the same. All of these terms are just constrained pointers with funky syntax, otherwise.
2) You cannot say that one thing is implemented in terms of another, therefore the same - virtually all high level constructs are implemented in terms of lower ones and it would be pointless to ignore the layer of abstraction provided.
3) You cannot point at an example of something both a and b can do. No amount of examples will show that a and b are the same in power/equivalent, only an exhaustive listing of capabilities would do. It takes but a single example to show a and b are different though, as I've done above.

Notice this table makes clear (though it misses several rows), that C++ references are strictly more limited than Java references, which are strictly more limited than C++ pointers (limited in the sense of available operations).


PS: Bonus points Mikademus and Average Software for their outstanding use of straw man arguments. Yeah, I'm a jerk for taking sides.
Logged
Mikademus
Level 10
*****


The Magical Owl


View Profile
« Reply #46 on: May 03, 2009, 02:50:02 PM »

PS: Bonus points Mikademus and Average Software for their outstanding use of straw man arguments.

I know the word "straw man" is in vogue in the 'net atm, but I am actually not aware of having made any here...?

PS. Creds for slick table.

[edit] Sure they are different in behaviour: they are defined to be different. So? Java's "references" are still recognisably pointers, even in C++ terms:

* you allocate with new
* you assign and reassign *targets* with = (note that string syntax is a special exception explicitly defined in the language standard).
* you can have NULL pointers (and the associated exception is even called "NULL pointer exception")
* there's probably more but I am sleepy
« Last Edit: May 03, 2009, 02:56:30 PM by Mikademus » Logged

\\\"There\\\'s a tendency among the press to attribute the creation of a game to a single person,\\\" says Warren Spector, creator of Thief and Deus Ex. --IGN<br />My compilation of game engines for indies
BorisTheBrave
Level 10
*****


View Profile WWW
« Reply #47 on: May 03, 2009, 02:57:28 PM »

Everything you can do with a reference, you can do with a pointer, but not vice versa.
Nope.
You then go on to assume the exact opposite of what muku said, by demonstrating things you can do with a pointer but not a reference. It's possible I misunderstood (I'm definitely not perfect), but I cannot see anything factually wrong with the above statement of muku's, even after reading your post. (assuming here e meant C++ references, which is what you assumed too).
Logged
BorisTheBrave
Level 10
*****


View Profile WWW
« Reply #48 on: May 03, 2009, 03:02:17 PM »

In response to the edited part:
Reread (3). No amount of similarities prove things to be the same. But once difference makes them different. Pointer arithmetic is the big difference here. I would certainly agree java references have more in common with C++ pointers than C++ references, but I don't think their usage of the term is too misleading. The fact is outside of C++, very few languages have a wide variety of redundant constructs and concepts, and so it matters a great deal less to be precise in what you mean - it is going to make sense to users of that language regardless.
Logged
Mikademus
Level 10
*****


The Magical Owl


View Profile
« Reply #49 on: May 03, 2009, 03:20:52 PM »

Well, your straw man argument might be spot on.

From a vivid debate on pointers and reference:
Quote
Dan: notice the thread title : )  I don't think there is any generally accepted notion of "pointer" and "reference" outside the context of C++.  If someone refers to those concepts in general, then I don't know what they mean (specifically).

(Not that that debate really has anything to do with us right now, I just wanted to grab that quote to show that we're not thinking new and unique thoughts here.) And it was the C++ context that Java constructs were named for, and a FUD straw man that Sun set up when declaring the evil and bug-prone nature of pointers wherefore they'd only use the safe good references. The Java naming of language facilities were intentionally misleading marketing (same as other arguments at the time of the birth of Java, f.i. MI and Operator and function/method overloading).
Logged

\\\"There\\\'s a tendency among the press to attribute the creation of a game to a single person,\\\" says Warren Spector, creator of Thief and Deus Ex. --IGN<br />My compilation of game engines for indies
Glaiel-Gamer
Guest
« Reply #50 on: May 03, 2009, 03:39:04 PM »

references and strings are the same thing

Code:
class Reference {
public:
    bool operator==(const std::string & str) const{
        return true;
    }
};
Logged
Average Software
Level 10
*****

Fleeing all W'rkncacnter


View Profile WWW
« Reply #51 on: May 03, 2009, 05:40:43 PM »

Pointer arithmetic is the big difference here.

If lack of pointer arithmetic makes something "not a pointer," then you would have to say that void* are not pointers.  Possibly function pointers and member pointers too (I didn't test those).
Logged



What would John Carmack do?
muku
Level 10
*****


View Profile
« Reply #52 on: May 03, 2009, 11:15:23 PM »

That's part of the constraints I was talking about. Everything you can do with a reference, you can do with a pointer, but not vice versa.

Nope. Using C++ semantics for illustration:
(...snip...)

Well, yes. As Boris said, you just confirmed my point.

Average Software, on the other hand, had a point with his example. (Not the string one, that was just insulting.) I'll still say that the "reference without storage" case is a specific local optimization that the compiler can only do when both objects in question are in the same scope; that breaks down as soon as you start passing references to functions or storing them in classes. (The compiler could theoretically also do the same thing for pointers for which it can statically prove that they always point to the same thing, as well as that they are never accessed by their value, only dereferenced. No fundamental difference.)

I think the main point to take away is that it doesn't really make sense to argue about nomenclature outside of a specific context, because every language defines them differently. Saying that Java references aren't really references is just silly because well, that's how Java defines references. In fact it seems the general CS term 'reference' actually contains pointers and C++ references as specific examples.
Logged
Movius
Guest
« Reply #53 on: May 04, 2009, 01:03:25 AM »

what the fuck is wrong with you people?
Logged
BorisTheBrave
Level 10
*****


View Profile WWW
« Reply #54 on: May 04, 2009, 01:26:58 AM »

This is what's wrong: http://xkcd.com/386/
I'm a serious sufferer.
Logged
muku
Level 10
*****


View Profile
« Reply #55 on: May 04, 2009, 01:36:37 AM »

also, common side effect of having a technical mindset; please disregard
Logged
Average Software
Level 10
*****

Fleeing all W'rkncacnter


View Profile WWW
« Reply #56 on: May 04, 2009, 05:02:27 AM »

Average Software, on the other hand, had a point with his example. (Not the string one, that was just insulting.)

It wasn't meant to be insulting and I'm sorry if you took it that way.  It was meant to show that your strict-subset thing doesn't hold water.

This stupid debate comes up so many times, and I think I know why.  It's the syntax.  People see the & and immediately think "address of" and they start thinking about pointers (why don't they think of bitwise-and?  That would lead to more interesting arguments).

I've often wondered if a different syntax would prevent the confusion.  The only other language I know of that has something akin to C++ references is Ada.  Take a gander at this Ada code:

Code:
I: Integer;
R: Integer renames I;

Pretend you know nothing about C++, and this is your first exposure to something like this.  Would that code make you even consider that pointers were involved?  I know I wouldn't think so.

What I also cannot fathom is why (and this isn't directed at anyone here, I see this time and time again all over) people insist on arguing with language standards.  It's like playing rock, paper, scissors, dynamite.  The dynamite always wins and you don't have it.

The C++ standard is C++.  Nothing else is C++.  That's why we have a standard.  The standard does not say that references are pointers, therefore you cannot assume that they are.  That should be the end of the discussion.  Dynamite beats everything.

Likewise, the Java standard says that references are pointers.  Same deal, the standard wins, you lose.  Java references are pointers.  Even if you have some impassioned argument for why they aren't, I will quote the Rock and say, "it doesn't matter what you think!"

The moral of the story is, you can't argue with language standards, unless you're on the standards committee.  If the standard says it's so, it's so, and no amount of rationalizing will ever change that.
Logged



What would John Carmack do?
BorisTheBrave
Level 10
*****


View Profile WWW
« Reply #57 on: May 04, 2009, 06:20:09 AM »

Man, that doesn't show anything. My index finger is a pointer, too. "pointer" is just a word, in English. The java docs doesn't say Java Reference = C++ Pointer, and that is what is what everyone else has been discussing. This is why I've prepended the word reference and pointer with the language context, as otherwise it's confusing when you mean what.

The java docs calls Java references "references" frequently, as well as "pointer" occasionally. Are you suggesting it is both equivalent to a C++ reference and a C++ pointer? That dynamite doesn't exactly hold water.

Also, not that I know Ada, but it's renaming facility is decidedly not the same thing as any of the of the previously discussed constructs.
You cannot do.
R: Integer renames MyFunction()
i.e.
int& R = MyFunction();
Correct me if I'm wrong, but rename doesn't use pointers under the hood, it seems entirely a static thing.

If you want a language which has something very similar to C++ references, I believe (early versions of) Fortran would do, where every variable is like that. On declaration, they are initialized to a static block, and pointers to that block are passed around every time you pass the variables as arguments, it uses the pointers. (there is syntax for initializing a "reference" to point elsewhere, but I forget it).
Logged
muku
Level 10
*****


View Profile
« Reply #58 on: May 04, 2009, 06:38:16 AM »

It wasn't meant to be insulting and I'm sorry if you took it that way.  It was meant to show that your strict-subset thing doesn't hold water.

Your logic was flawed though. You can use a reference to indirectly refer to another object, which you cannot do with a string.

Quote
The C++ standard is C++.  Nothing else is C++.

If I may play devil's advocate: to the best of my knowledge there is no compiler which is compliant with the C++ standard, so you are saying that C++ is a language which exists only on paper Wink
Logged
Average Software
Level 10
*****

Fleeing all W'rkncacnter


View Profile WWW
« Reply #59 on: May 04, 2009, 06:50:38 AM »

If I may play devil's advocate: to the best of my knowledge there is no compiler which is compliant with the C++ standard, so you are saying that C++ is a language which exists only on paper Wink

The Comeau and Intel C++ compilers are compliant, to the best of my knowledge.  They even support export templates.
Logged



What would John Carmack do?
Pages: 1 2 [3] 4
Print
Jump to:  

Theme orange-lt created by panic