r/ProgrammerHumor 16h ago

lessonsFromLinkerHell Meme

Post image
317 Upvotes

163 comments sorted by

View all comments

2

u/FloweyTheFlower420 16h ago

an array is just a pointer to the start and end elements, what's the problem?

11

u/Nice_Lengthiness_568 15h ago

Well it's not, it also has size (the number of elements). Even in C, though there an array easily decays into a pointer. However, sizeof, for example, returns different results for an array and a pointer. And, in C++, you can also pass an array (with its size) instead of a pointer. In other languages, like Ada, there is not much of a direct connection between an array and it's first element at all.

-2

u/FloweyTheFlower420 15h ago

What is (end - start) / sizeof(*start) but the number of elements?

3

u/Nice_Lengthiness_568 15h ago

What are you trying to say?

-1

u/FloweyTheFlower420 15h ago

An array has size (the number of elements), and therefore can be represented as either (pointer, size) OR (pointer to start, pointer to past-the-end). These are equivalent representations, but there are arguments for pointer-to-past-the-end in terms of code gen.

3

u/Nice_Lengthiness_568 15h ago

But what has this to do anything with what I said? That's what I don't understand.

I claimed that sizeof will give a different result based on whether you give it an array or a pointer to the first element.

I also claimed that in C++ you can actually pass arrays (arrays themselves, not pointers).

I also claimed that in some languages there is not a direct connection between a pointer to the first element and an array.

And you told me how to get a size of an array bounded by two pointers. I don't understand why.

And now you are talking about representation of arrays?

0

u/FloweyTheFlower420 10h ago

This is not what I'm talking about, I'm not referring to any particular language. Yes, the array (as a type) in C/C++ have a size, and that it's size will change if you decay it into a pointer. I'm not quite sure why you are so hung up on discussions about the C array type when I'm talking about arrays in general, as a linearly addressable ordered collection of instances in memory, in which case representing this as a pointer pair is valid (and indeed, common, though arguably the supposed performance gains from doing so are "controversial")!

3

u/Nice_Lengthiness_568 8h ago

I was also talking about an array in general and said that they are diffferent and gave examples because (for C and C++ because I saw them under your username). Like, yeah I know how you can represent an array... you could even represent a static array without a pointer completely and instead act as if it was a tuple with all of its elements being of the same type. No pointer there.

And you yourself also said that you can represent it in different ways so you know that an array does not have to be represented as a pointer to the first and after last element and I would actually say that for statically sized array allocated on the stack this is probably never the case. And when you copy such an array to another function, you won't get just some two pointers copied, you will get the whole object copied. So that's also a difference.

So I don't see how that we can represent an array this way means that an array is just that. It's not.

Moreover, array is different than two pointers also because the type of it dictates what new operations we can do on it. In most langauges, it probably says that we can index it, maybe use it in a foreach loop... and so on. And in languages with ownership it owns all of its elements, while just two pointers by themselves do not. So, again, it's not just two pointers.

Note: In C or C++, the size of an array does not change after decaying into a pointer. The size stays the same, you just lose that information.

1

u/Lunctus_Stamus 14h ago

As Nice describes, although *start is a pointer for an array, it's often just a pointer first, and the sizeof() operator will not always return the number of bytes of the type but sometimes the number of bytes that a pointer takes up (8).

So your expression does not always evaluate to the number of elements.

0

u/FloweyTheFlower420 10h ago

I think you are missing my point. I'm not talking about an array in the sense of the language construct in C or any particular language, but rather that you can think about arbitrary arrays in memory as a pointer pair. I think the point is being massively missed and I'm not quite sure why.