I'm sure I'd once seen a definition of sizeof() somewhere :-)
Anyway, my point was that these constructs are not functions, there are no language guarentees for their behaviour if you treat them as if they were.
Actually you can rely on sizeof
How big is this?
struct AudioObject {
unsigned long numSampleFrames;
unsigned short sampleRate;
...
signed short waveData[1]; /* memory beyond this point is wave data */
};
I've seen such "open ended" structures in a lot of places. Of course sizeof() cannot know you might have malloc()'ed this structure with an additional 200K worth of sample space.
And I've still seen people wonder why sizeof() didn't work for it, along with more comical examples, eg using sizeof() instead of strlen() for non-constant strings (and worse) :lol:
So, you can only rely on sizeof() when you fully understand its necessary limitations.
Well isn't that quite obvious, huh?
To you, yes. Never underestimate the ability of people to overlook the obvious however. I have seen code where I work that would give you hives.
C++ isn't C.
No, it's better in every significant way :-P
Seriously, that one word retort doesn't detract from the observation that sizeof() has no knowledge of the element actually pointed to. It simply trusts the developer to know what he has passed to it when dereferencing.
Consider the typical pointer casting frenzy that is the stock of amiga C coding. Often the pointer you deal with is not the genuine type of object pointed to, such as Task* v Process* . Getting the size of a Process by dereferencing a pointer that happens to be a Task* will return the size of the struct Task, not the struct Process.
You might sit there and complain this is totally obvious and I'd agree, but again people do make such silly assumptions, especually when they see such casts being used so loosely. You and I know that Process embeds a Task as its first member and therefore are not the same (or even comparable) structures, but it might not be as obvious to someone fairly new to the language.
In C++ the problem is compounded with abstract types as I describe and I have seen people who really ought to know a lot better attempt exactly what I demonstrated in that example.
So, obvious or not, such mistakes are made.