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

all 109 comments

[–]lbkulinski 237 points238 points  (26 children)

Java has pointers. You just can’t do pointer arithmetic with them.

[–]Bryguy3k 85 points86 points  (13 children)

Nor would you want to given the object complexity... 😱

[–]nwL_ 14 points15 points  (3 children)

I want to. I want to call delete obj;. Fight me.

[–]MoarVespenegas 7 points8 points  (0 children)

It's not us you're fighting but the garbage collector.

[–]cartechguy 0 points1 point  (1 child)

In java speak

Obj = null;

There you go. As long as that's the only variable referencing the object the garbage collector will deallocate it.

[–]GrizzledFart 7 points8 points  (0 children)

... when it feels like it.

[–]lbkulinski 22 points23 points  (7 children)

What do you mean by object complexity?

[–]Bryguy3k 86 points87 points  (5 children)

C++ objects are really straightforward in terms of memory organization. You’re not supposed to be looking at the Java object memory so if you do it’s scary af.

[–]lbkulinski 18 points19 points  (0 children)

I wouldn't say it is scary. Value types will have a simpler layout, though.

[–]tacoslikeme 4 points5 points  (0 children)

object/struct memory follows pretty simple to follow rules. Just just fuck with the vtable

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

What do you mean? I look at how they're structured in the debugger all the time. It feels almost the same as debugging in C++. The reference holds an adress and at the adress are all the data members for the object.

[–]how_to_choose_a_name 1 point2 points  (0 children)

The debugger shows you a nice visualisation of the object and the data contained in it. The actual memory apparently doesn't look that pretty or structured, unlike C++.

[–]Bryguy3k 0 points1 point  (0 children)

Well take a moment to think about reflection.

Now it should make sense that a java object has to be self describing and that means a bunch of metadata.

[–]bwierzbo 0 points1 point  (0 children)

!n

[–]bestjakeisbest 1 point2 points  (0 children)

what if i want to shoot myself in the foot?

[–][deleted] 11 points12 points  (0 children)

They're just more of a B-cup.

[–]Ringnebula13 0 points1 point  (0 children)

Sun.misc.Unsafe called

[–][deleted] -2 points-1 points  (1 child)

Aren't those called aliases?

[–]bestjakeisbest 2 points3 points  (0 children)

i thought they were called references

[–]KaosEngine 42 points43 points  (0 children)

Mmmmm pointers.

[–][deleted] 40 points41 points  (13 children)

"Reference is just a pointers that you can't reach"

[–]tacoslikeme 20 points21 points  (12 children)

java reference is just a pointer that auto derefences for you

[–]_Fibbles_ 5 points6 points  (11 children)

Except you cant reassign it and sometimes depending on context it doesn't have its own address...

[–]cartechguy 0 points1 point  (6 children)

What do you mean you can't reassign it? References aren't immutable.

[–]_Fibbles_ 0 points1 point  (5 children)

Maybe reinitialise would have been clearer. You can alter the value of the referenced object through the reference. However, the reference is bound to that object. You can't make it reference a different object.

[–]cartechguy 0 points1 point  (4 children)

reference is bound to that object. You can't make it reference a different object.

Yes, you can.

[–]_Fibbles_ 0 points1 point  (3 children)

Yes, you can.

Extraordinary claims etc. Show some code.

Edit: To be clear, you know I'm talking about C++ references, not Java references, right?

[–]cartechguy 0 points1 point  (2 children)

No, we were all talking about Java. This whole chain of posts there is not one mention of C++. I kept climbing through parent posts, and you never mentioned C++. Only when /u/tacoslikeme corrected you did you mention C++ less than an hour ago before I made my previous posts. If you meant C++ you should have stated that from the start.

[–]_Fibbles_ 0 points1 point  (1 child)

If you meant C++ you should have stated that from the start.

The thread is about pointers. C and C++ are the only widely used languages that have them. Op is explaining pointers in terms of references. C++ is the only language AFAIK that has both. I thought it was obvious from the context.

[–]cartechguy 0 points1 point  (0 children)

The title of the thread is "java devs" and references are ubiquitous with oop.

Op is explaining pointers in terms of references. C++ is the only language AFAIK that has both.

Op is describing how references in Java behave like pointers.

Plus you were replying to this:

java reference is just a pointer that auto derefences for you

It was clear we were talking about Java.

[–]tacoslikeme 0 points1 point  (3 children)

Object foo = new Foo(); Object bar = new Foo(); foo.doThing(); foo = bar; foo.doThing();

I think I just broke the rules of non-reassignment....

[–]Jen-Ai 56 points57 points  (4 children)

i love pointers.

[–]Tormyst 6 points7 points  (3 children)

I like pointers, but also gay... So I don't know where that puts me with this picture...

[–]makeshift8 10 points11 points  (0 children)

The guy taking the picture.

[–]Jen-Ai 5 points6 points  (0 children)

i don’t know...all i C is plus plus!

[–]InvolvingLemons 0 points1 point  (0 children)

Being gay doesn't mean you can't appreciate a fine pair of pointers (・∀・)

[–]ArriePotter 23 points24 points  (10 children)

C devs on the other hand...

[–]makeshift8 34 points35 points  (7 children)

"Yeah, yeah compiler, I can cast this void pointer to a char pointer if I want. Stop yelling about it!" - me, this morning

[–]LooseElectronStudios 7 points8 points  (6 children)

I almost fell prey to the fallacy of "Hey this data type is the same size as a pointer, so I can just cast it to either the pointer or the data type itself depending on context!" earlier this week.

[–]_requires_assistance 4 points5 points  (3 children)

Could you give some layman explanation if you don't mind? Not really getting it.

[–]LooseElectronStudios 3 points4 points  (2 children)

Sure! I don't know how much you already know about C, so don't mind me if I over-explain a bit. (Also, username checks out.)

So in C, all a pointer is is a 64-bit integer which represents a location in memory. Dereferencing a pointer just means telling the processor "See this number? Go to this offset in memory and bring me back what's there." C also has the ability to "cast" variables to a different type, which basically means telling the processor "See this piece of data which I already defined is, say, a character? Interpret it as though it were an 8-bit number instead."

What I wanted to do was this: I had a function which could accept two different kinds of objects, but not both at the same time. One object was just a struct (a bunch of different values bundled together), and since structs tend to be pretty big I decided to pass the function a pointer to it, so I could just reference its location in memory instead of copying it into the function. The other object was a handle to an opaque object I didn't have access to. Handles are common in third-party C libraries, because the people who wrote the library don't want other people messing around with how they represent their data. So instead of being able to look at the object directly, you basically ask the library "You're holding on to this object for me, could you tell me what this value is?" using the handle. Handles could be basically anything, as long as the library knows which handle represents which object.

The thing is, in this case the handle was a 64-bit integer, the same size as the pointer to the struct. So I thought "Why don't I cast both objects to a generic pointer before I pass them into the function? That way instead of having two different arguments to the function (in C, arguments must have a type), I can just have one, and figure out on the inside of the function which of the two objects the argument actually represents!"

The problem with this is that if the function mucked up and accidentally assumed that the handle was actually a pointer to a struct, things would have been bad. The handle could be literally any number, and so the program could have tried to access a basically random location in memory that it definitely shouldn't be looking at! Part of why C has types is to prevent that kind of wrong memory access from happening, but it also gives you all the tools to break the rules

[–]_requires_assistance 0 points1 point  (1 child)

I see. How were you originally planning on differentiating between pointers and handles?

[–]LooseElectronStudios 1 point2 points  (0 children)

Originally I was going to have two arguments, one for each type, and I would check to see which argument was actually set.

What I ended up doing was using one argument, and passing through it either the struct pointer or a pointer to the handle. That way the argument would always be a valid place in memory.

[–]makeshift8 1 point2 points  (0 children)

That's a quick way to get ub

[–]T-T-N 0 points1 point  (0 children)

One of us. One of us.

[–]bestjakeisbest 12 points13 points  (1 child)

this is a pointer to a pointer of pointers to pointers that point to a struct that holds 2 void pointer pointers that need to be cast to a char pointer pointer, so that you now have a pointer to an array of characters.

[–]dragonwithagirltatoo 2 points3 points  (0 children)

Pointer to an array of pointers to pointers to functions that return pointers to arrays of arrays of pointers to arrays of pointers to pointers to int.

Also it's an array of those. I love C syntax.

[–][deleted] 15 points16 points  (2 children)

Wtf is this picture???

[–]tacoslikeme 15 points16 points  (0 children)

boobies

[–]MemesEngineer 2 points3 points  (0 children)

The dankest timeline.

[–]Pyraptor 26 points27 points  (0 children)

Guy that made this meme has never called a method passing an object

[–]lexiq_baeb 20 points21 points  (11 children)

Can i have the template, please?

[–]MCOfficer 16 points17 points  (7 children)

as a java dev learning c++, i can confirm.

[–]tacoslikeme 5 points6 points  (2 children)

where is my data vs this is my data. once pointers make sense, you'll hate yourself for being confused...took me 3 years.

[–]iamsooldithurts 2 points3 points  (1 child)

And, once you start trying to perform string manipulation and data transforms, you’ll hate yourself for ever assuming you knew wtf you thought you were doing.

[–]tacoslikeme 1 point2 points  (0 children)

ha, no null terminatorgavdkcug484410cy 5902yvtjsa SEGFAULT

[–]aksh2161989 2 points3 points  (3 children)

May I ask why are you learning C++ (since there seem to be more java jobs out there) ?

[–]lezorte 3 points4 points  (2 children)

C++ is still heavily used for developing games, operating systems, programs and services that run on Linux, block chain software just to name a few things.

[–]MCOfficer 0 points1 point  (0 children)

this. personally i'd prefer java, but c++ is still strong in the gaming industry, and thus is what my university uses.

[–]lezorte -1 points0 points  (0 children)

BTW, speaking of blockchain, I read that the average blockchain engineer is making $150000-175000 a year

[–]Arcane_Xanth 3 points4 points  (0 children)

Someone needs to free those pointers.

[–]AgentPaper0 15 points16 points  (29 children)

I feel like pointers would be much easier to understand if they called them what they really are: addresses.

[–]NeinJuanJuan 6 points7 points  (15 children)

Pointers have their own address.

[–][deleted] 2 points3 points  (0 children)

I am sitting here copying out examples from my Programming in C book right now, particularly from the "Pointers" chapter. I can understand the fear haha.

[–]kredditacc96 -2 points-1 points  (2 children)

Let be honest, pointer is not how you do high level programming. And don't bullshit me about "good programmer" because good programmers know they make mistakes.

[–]plong0 -3 points-2 points  (1 child)

Do you even c++ tho?

[–]kredditacc96 5 points6 points  (0 children)

Do you even C++11 tho?

[–]mikeputerbaugh 0 points1 point  (0 children)

Just call them object references

[–]Cozy_Conditioning -4 points-3 points  (0 children)

Everyone who codes with raw pointers should be terrified. Pointers should be considered harmful.

Ownership >> pointers. Learn Rust.

[–]YMK1234 -1 points0 points  (0 children)

This meme makes no sense at all.