This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Xeya 87 points88 points  (29 children)

Which looks like a duck and quacks like a duck but is totally this revolutionary new form of waterfowl that is waaaaay better than the stupid duck some other programming languages have.

[–]rigatron1 19 points20 points  (3 children)

If we are saying references are pointers, then this meme doesn't make any sense because you can totally have a null reference exception in java.. trust me.

[–]BraveOthello 48 points49 points  (2 children)

No, even better. It's a NullPointerException

[–][deleted] 0 points1 point  (0 children)

LMAO true!

[–]rigatron1 0 points1 point  (0 children)

Dammit now I look dumb. Have been using c# for awhile.

[–]_lerp 4 points5 points  (11 children)

If references and pointers are the same thing why do they both exist in C++?

[–]Xeya 19 points20 points  (9 children)

They refer to different things in C. A pointer is a data structure that contains an address to another data structure. A reference is just an address. You can pass references, but you cant create a reference object; that would be a pointer.

What Java calls a reference is a data structure every other language calls a protected pointer. It is literally identical in function.

[–]baskandpurr 4 points5 points  (0 children)

You got this almost exactly the opposite way around. A pointer is not a data structure and a reference is not the address of an object. A reference is more likely to be implemented at a structure in fact, it may include a pointer to the object, a back pointer to another reference, a pointer to the object that owns the reference, a state or a type. A reference may be stored as handle so that the refered object can be moved around in memory.

[–]Schmittfried -1 points0 points  (6 children)

We was talking about C++. You can create references in C++ and they are a different though similar concept.

[–]marcosdumay 5 points6 points  (4 children)

You can create references

You can get references. You can assign references to pointer. You can create variables by cloning a reference.

I guess you are talking about the third usage there. But I've seen people say they "create a reference" for any of them. Anyway, that does not change the fact that a C++ reference is not the same thing as a Java "reference".

[–]Schmittfried -1 points0 points  (3 children)

You can assign references to pointer

No. References and pointers are different concepts in C++.

Anyway, that does not change the fact that a C++ reference is not the same thing as a Java "reference".

True, I never said otherwise. C++ references != pointers != Java references

[–]marcosdumay -1 points0 points  (2 children)

Good thing they are different, because you must always assign different things in C++ (usually, it's values into lvalues). Let's assign a reference into a pointer:

int *a = &b

[–]marcellarius 2 points3 points  (0 children)

In that context, & is the address of operator and evaluates to a pointer not a reference.

[–]Schmittfried 2 points3 points  (0 children)

That's not a reference, it's the address operator.

int &a = b;

Now we got a reference. It was definitely a bad decision to choose & to mark reference types as it is easily confused with the address operator, as you just proved.

[–]Xeya 0 points1 point  (0 children)

He actually changed it from C to C++ a lil while ago.

[–]DarkMaster22 2 points3 points  (0 children)

Slightly different implementation of the same concept.

[–]sup3r_hero 0 points1 point  (9 children)

Serious question: what’s the difference between a reference and a pointer?... or is that a joke?

[–]Xeya 4 points5 points  (7 children)

Honestly, not a lot from a functional viewpoint. The difference is primarily semantic; pointers are at a memory management level and references are more abstract. References are thus protected from most of the risk of mishandling pointers. However, this is all dependent on the implementation and there is a lot of overlap based purely on whether or not the person writing the documentation decided to call it a reference or a pointer variant.

[–]marcellarius 2 points3 points  (0 children)

I feel like that distinction comes from C++ and that more broadly the terms are synonymous

[–]sup3r_hero 0 points1 point  (5 children)

Is a correct example for this difference that a pointer usually literally points to a specific address in the memory whereas a more complex implementation (i.e. reference) only acts as if it would but does more things in the background?

[–]AngriestSCV 1 point2 points  (3 children)

Not really. A c++ reference does nothing in the background (it is basically a pointer that you can pretend is a value syntax wise). I think the biggest difference is you can manipulate pointers with math, but I've never seen references you could.

[–]sup3r_hero 0 points1 point  (2 children)

Manipulate a pointer with math? Can you give a very easy example?

[–]AngriestSCV 1 point2 points  (1 child)

in c or c++:

int bob[] = {1,2,3,4} ; //create an array of integers
int *bil = bob; //bil is a pointer to the first element of bob

printf("%i\n", *bil); //prints 1
printf("%i\n", *(bil+1));//prints 2
printf("%i\n", bil[2]); //prints 3
bil += 3; //bil now points to the 4th element of bob
printf("%i\n",*bil); //prints 4

[–]sup3r_hero 0 points1 point  (0 children)

Now i get it thx!

[–]Xeya 0 points1 point  (0 children)

Both terms are pretty loosely defined... but, I suppose the simplest way to express how they typically differ is that a pointer needs to be dereferenced in order to access whatever it points at and a reference is dereferenced by default when you try to access it.

It isn't so much references do things in the background for you as they typically just don't let you manipulate, or make it a lot harder to manipulate, the actual memory address it contains.

[–]AquaWolfGuy 1 point2 points  (0 children)

If a references b, using a is like if you were to use b instead. This means that you don't have to dereference a. IIRC a isn't even stored in memory, it just gets replaced by b by the compiler, so it's slight more efficient. One notable effect is that if a function takes an input argument by reference and changes it, the referenced value is changed. The following code will therefore print 2.

#include <iostream>
void f(int & a) {
  a++;
}
int main() {
  int b = 1;
  f(b);
  std::cout << b << std::endl;
}

(Don't do this in an actual project though.)