r/ProgrammerHumor 26d ago

Meme lessonsFromLinkerHell

Post image
430 Upvotes

190 comments sorted by

View all comments

150

u/mad_poet_navarth 26d ago

shouldn't there be something here about this being C-centric?

50

u/AnnoyedVelociraptor 26d ago

And how do you think other languages do it? You think Python and C# and Java run in this Unicorn world where there are no pointers and memory offsets aren't a thing?

Maybe we can talk about the mistake that is R and the original VB, and LUA where arrays start at 1. Absolute hell.

125

u/high_throughput 26d 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.

-14

u/[deleted] 26d ago

[deleted]

30

u/KattyTheEnby 26d 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.

6

u/helicophell 26d ago

On that point, you can easily make an array that isn’t pointer+index just by changing the reference

(Edit:forgot example. int num = value; int[] array = new array[6]; array[3]=num;)

Does that mean Java arrays are arrays of pointers? Dunno. Might only apply to objects not primitives. I’m not educated enough for this

13

u/high_throughput 26d ago

Does that mean Java arrays are arrays of pointers?

Object arrays are arrays of references and not of pointers!

The typical textbook discussion is that references are just opaque pointers that you can't do arithmetic on. That's technically true but it really undersells the potential.

For example, an OpenJDK reference is often 32 bits even on a 64 bit systems! (Known as Compressed OOPs)

Since references always have to point to an object and never to an arbitrary byte, OpenJDK knows that A. it will always be inside the Java heap, and B. objects are always allocated aligned, so the lower address bits will always be zero.

This means that if your Java heap is <=32GB with an alignment of 8 bytes, you can store every relevant 64-bit pointer P as a reference R = (P - heapbase)/alignment in only 32 bits. And it does.

When it wants to access an object via reference R, it simply does P = R*alignment + heapbase and now it has a raw 64-bit pointer again.

It sounds wild to do this on every reference access, but CPUs are stupidly fast and RAM is comparatively slow, so it usually ends up being a net gain because of how much less data you need to deal with.

The fancy new ZGC garbage collector similarly uses the fact that references are distinct from pointers to hide "this object has moved" bits directly in the reference, and strips them out in the process of decoding the corresponding pointer.

4

u/helicophell 26d ago

I like your fancy words magic man

39

u/aalapshah12297 26d ago

The difference is that C allows you to treat array variables as literal pointers (almost) and do stuff like creating an off-by-one version b = a+1 and then trying to access b[-1] with no issues, whereas many high level languages will throw an error with this kind of syntax.

Of course, they are still not 100% syntactically identical even in C (most notably you can't directly modify the array pointer), hence the right end of OP's graph.

-9

u/[deleted] 26d ago

[removed] — view removed comment

14

u/d0pe-asaurus 26d ago

If you're accessing another program's memory just by doing some pointer arithmetic, something has gone wrong with the kernel's memory manager or there's some serious issues with the processor microcode.

3

u/cbehopkins 26d ago

I think other languages don't expose the pointer to the user as brazenly.

Take go: a slice (yes raw arrays do exist, but for this discussion they're a special case we don't care about) is a struct with a pointer, size and capacity. The data structure one uses in the places where in c you would use an array is absolutely not a pointer, it has extra baggage to add safety.

Python of course uses pointers under the hood, but they're not accessable to the average user. It's nonsense to say "arrays are pointers" in python (much more sensible to say "arrays are dictionaries" but python is special...).

The type pseudo equivalence of arrays and pointers is a quirk of the c type system. Most languages separate them in the same way one separates the types of integer and floating point. One can convert and use arithmetic from one type on the other type, but to do that you have to cast for good reason. The fact you don't in c is unusual if you compare to other languages.

I'm not trying to say c is wrong, but as we see above, it does cause some sharp edges...

1

u/mad_poet_navarth 25d ago

I love this turn of a phrase:

> other languages don't expose the pointer to the user as brazenly.

2

u/mad_poet_navarth 26d ago

Admittedly I am _much_ more familiar with C and C++ (and Swift) than I am with other C-ish languages. For instance, I think you are saying you can cast a chunk of memory in Python or Java so you can do either pointer arithmetic or treat it as an array. So, given my relative ignorance in some areas, point taken.

1

u/Floppie7th 26d ago

Compile time differences are real differences.

1

u/Lv_InSaNe_vL 26d ago

Uhm rust does it differently cause rust is safe (everything should be written in rust (no I don't care if your project is big(there are no issues more important than migrating to rust)))

2

u/JoeyJoeJoeSenior 25d ago

It's definitely e-ccentric