sizeof a struct element

Hal Murray halmurray at sonic.net
Thu Sep 12 06:35:49 UTC 2024


Fred Wright said:
> You can just use sizeof().  The only caveat is that there's no way
> (AFAIK)  to apply it to an element of an abstract type, so you need a
> concrete  instance to apply it to.  But the concrete instance can even be
> just a  pointer, as long as it's a pointer to a complete struct type.

Thanks.  That's exactly what I was looking for.  Interesting that I didn't 
think of it.  Maybe because I was looking at code that does sizzeof(<type>
).


> Resorting to configure tests just to get type sizes is in general a bad
> idea, for aforementioned reasons. 

I'm no fan of #if.  This case is an interoperability issue.  The man page 
says long, but the actual implementation can be either 32 or 64 bits on a 
32 bit Linux box.

The context is the server side of mode6/ntpq.  It does a lookup on a name 
and "prints" the ascii format of that variable.  I have a table with name, 
type, and pointer for each slot and a macro to make a slot for each type.  
Unless it needs a new type, adding a new name/variable, aka slot in the 
table, is a 1 line edit.  (The old code required edits in 3 separate 
tables.  You couldn't easily do an insert or move a slot.)  The struct for 
the slot has a giant union for the pointer, one for each type.  That union 
does type checking which I consider essential.

There are only 5 or 6 of this type of variable.  It's probably less code 
overall if I make special types for each one and that avoids needing to 
know the size.  Thanks for making me think more about this tangle.


-- 
These are my opinions.  I hate spam.





More information about the devel mailing list