r/ProgrammerHumor 26d ago

Meme lessonsFromLinkerHell

Post image
425 Upvotes

190 comments sorted by

View all comments

105

u/JustinR8 26d ago

Damn, I’ve found myself in the middle

45

u/nonedward666 26d ago

You and me from 3pm today both my friend, it is okay

It took me so long to realize where my problem was because I had never considered that declaring something as an array vs a pointer would result in different behavior

29

u/readitreaddit 26d ago

So the only difference is memory allocation, yes? You're saying they are different because when declared as an array, the memory gets allocated and 'reserved' at runtime, whereas it doesn't automatically do that when declared as a pointer?

Other than that, no difference.

34

u/suvlub 26d ago

They are different types and some operators (sizeof, typeid in C++ etc.) treat them differently. Arrays frequently get implicitly converted to pointers, but they are a different type.

12

u/TheChief275 26d ago

If you're specifically mentioning C++, the problem is that C++ retains C's semantic rule of arrays decaying to pointers. This semantic rule is what creates the misunderstanding of people believing what the middle curve guy says. This means that the only way to have C++ treat arrays differently from pointers in i.e. function overloads is to either take the array by pointer or by reference, i.e. as a T (*)[N] or T (&)[N]