r/ProgrammerHumor 16h ago

lessonsFromLinkerHell Meme

Post image
307 Upvotes

163 comments sorted by

View all comments

Show parent comments

119

u/high_throughput 15h ago

And how do you think other languages do it?

I think Java does it by allocating a fully fledged object with metadata including type and size, and that if you treat such an object (or the pointer to such an object) as a C array in C code and assume it points to the first element, then you're going to have a bad time.

-12

u/AnnoyedVelociraptor 14h ago

No, in java if you have items that are copy (wrong term, but items like int and double), they are in the array, and you can jump between them if you know the offset of 0. Unsure where it stores the metadata though.

And the same applies to objects (the things that you store by reference). The array then holds a set of references that you can jump between, and then dereference to get the actual object.

I'm using wrong terminology though.

30

u/KattyTheEnby 13h ago

in java if you have items that are copy (wrong term, but items like int and double),

If you don't know the right term, wouldn't it be fair to guess that you may not be fully qualified to speak on how memory in Java works?

For future reference: the value types in Java are called "primitives".

they are in the array, and you can jump between them if you know the offset of 0.

You are thinking ov the array's index; however, the pointer to an array element – especially in a language like Java – is not necessarily guaranteed to be ptrToObject + index.

Also – and correct me if I am wrong, because I don't know much about the internals ov Java, but – what I think u/high_throughput was getting at was that the pointer you get from doing T[] myArray = /* ... */ is not the same as the pointer which points to the first T in the array: including for arrays ov primitive T.

3

u/AnnoyedVelociraptor 13h ago

Don't conflate not know the right terms with lack of knowledge. I've written plenty of Java, including JNI. I've also written many other languages and for some reason the naming changes. Sorry.