Mail Archives: djgpp/2012/07/28/07:00:19
On 27.07.2012 22:09, Rod Pemberton wrote:
> I didn't know that, but I avoid sizeof().
Frankly, that's a pretty silly position.
> One could definately argue C shouldn't "know" anything about the padding,
> i.e., it can be argued that padding is outside of C's "sphere of knowledge".
Whether or not that argument can possibly make sense depends on what you
mean by "C" here: the C source code, the C translation tools, or some
combination of the two?
Yes, C source code must not know such things all by itself. But the C
tools obviously must know (or they couldn't support structs at all).
But sometimes the C source needs a way to inquire the info from the C
tools, e.g. to dynamically allocate (arrays of) structs. The sizeof()
operator is this way.
> Either way, I see sizeof() leading to problems.
You got that backwards. sizeof() is what solves those problems.
The actual problem is with people who believe they know better than the
C compiler what the actual size of a particular data structure must be,
and therefore come here (and elsewhere) to complain that their
compiler's sizeof() were buggy.
> There will be those who want to know the exact underlying size of the
> aligned and padded struct so they can allocate space for more aligned and
> padded structs correctly.
Those people have understood what sizeof() is for.
> There will also be those who only want to know only the size of the struct's
> C components so that when they copy the structs C elements to an array or
> other object they don't have extra space included, etc.
Such people have to sum the sizes of individual elements themselves.
- Raw text -