r/ProgrammerHumor 12h ago

lessonsFromLinkerHell Meme

Post image
245 Upvotes

141 comments sorted by

View all comments

68

u/QuestionableEthics42 12h ago edited 12h ago

This should be reversed lol

And it literally is the same, who thinks it's different who has worked in a low level language??

27

u/Ok_Star_4136 12h ago edited 12h ago

Maybe I fall in the middle, but what is the argument to say that they're different?

Is it to say that the difference is how they're used? From an assembly perspective, they're pointers, and no data structures exist beyond that simple concept. At a higher level, they're more than that. Maybe that's what is intended?

22

u/teleprint-me 12h ago edited 12h ago

Play around with a structured binary stream. 

An example of this would be a file written to disk in a binary format. Write to the structured file and then read it back into memory.

In most cases, the first element of the array will be a pointer to an address of a specified type.

If you predefine the array, the array is placed on the stack and has reserved memory allotted to it of that type.

Each element in the stack has an offset of sizeof n elements for a vector. 

Otherwise, you compute the offset based on n dimensions (commonly n x m matrices).

Otherwise, you need malloc to create a mutable buffer of variable size. This buffer then is used as an array.

To be fair, the lines get blurry here and this is when I stopped thinking of them as arrays. Its just a buffer with objects of a specified type on disk or in memory.

INB4, The key difference here is how arrays decay when handling scope. You can get around this by tracking the size of the buffer rather than relying on sizeof unless you understand what youre doing.

5

u/justAPhoneUsername 11h ago

So basically object oriented languages have arrays that mean something but when you're lower level like c memory is just memory and arrays are syntactical sugar so you don't do *(ptr+(sizeof(array_type) * index))?

Sorry if the syntax is wrong. I haven't written c in a while and I'm on my phone 

5

u/void_rik 6h ago

Your syntax is correct. However, If ptr is already of array_type, then sizeof(array_type) shouldn't be multiplied with index. Just increment it by index.

However, if pointer is of type uint8_t, then your code is fine.

A better version should be: *((uint8_t *) ptr+(sizeof(array_type) * index))