linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* any compelling reasons to use "offsetof"?
@ 2005-08-18 14:21 Robert P. J. Day
  2005-08-19  5:46 ` _z33
  2005-08-19  9:36 ` Glynn Clements
  0 siblings, 2 replies; 3+ messages in thread
From: Robert P. J. Day @ 2005-08-18 14:21 UTC (permalink / raw)
  To: C programming list


  in a legacy project i just inherited, i did a quick recursive grep
and noticed a number of invocations of "offsetof".  from experience,
i've concluded that the majority of these calls are because of sloppy
programming or laziness or just plain doing something incredibly hacky
that could be done in a more straightforward way.

  are there any really compelling reasons to need to know the offset
of a structure field?  just curious.

rday

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: any compelling reasons to use "offsetof"?
  2005-08-18 14:21 any compelling reasons to use "offsetof"? Robert P. J. Day
@ 2005-08-19  5:46 ` _z33
  2005-08-19  9:36 ` Glynn Clements
  1 sibling, 0 replies; 3+ messages in thread
From: _z33 @ 2005-08-19  5:46 UTC (permalink / raw)
  To: linux-c-programming

Robert P. J. Day wrote:
>   in a legacy project i just inherited, i did a quick recursive grep
> and noticed a number of invocations of "offsetof".  from experience,
> i've concluded that the majority of these calls are because of sloppy
> programming or laziness or just plain doing something incredibly hacky
> that could be done in a more straightforward way.
> 
>   are there any really compelling reasons to need to know the offset
> of a structure field?  just curious.
> 
I don't much about that macro. Anyway I came across a couple of articles 
from which I have a vague idea of why it is used.
<http://www.cepba.upc.es/docs/sgi_doc/SGI_Developer/books/CLanguageRef/sgi_html/ch01.html>
<http://www.freshsources.com/19950054.HTM>
Mostly, if the project is of ANSI standard, then it is just there for 
standards compliance. Otherwise, the macro is used for the reasons 
mentioned here in this article 
<http://www.embedded.com/showArticle.jhtml?articleID=18312031>.


_z33
-- 
I love TUX; well... that's an understatement :)


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: any compelling reasons to use "offsetof"?
  2005-08-18 14:21 any compelling reasons to use "offsetof"? Robert P. J. Day
  2005-08-19  5:46 ` _z33
@ 2005-08-19  9:36 ` Glynn Clements
  1 sibling, 0 replies; 3+ messages in thread
From: Glynn Clements @ 2005-08-19  9:36 UTC (permalink / raw)
  To: Robert P. J. Day; +Cc: C programming list


Robert P. J. Day wrote:

>   in a legacy project i just inherited, i did a quick recursive grep
> and noticed a number of invocations of "offsetof".  from experience,
> i've concluded that the majority of these calls are because of sloppy
> programming or laziness or just plain doing something incredibly hacky
> that could be done in a more straightforward way.
> 
>   are there any really compelling reasons to need to know the offset
> of a structure field?  just curious.

It's most commonly used in conjunction with an application-specific
object system or generics mechanism.

Probably the best-known example is Xt widget definitions. Each widget
class definition has an array of XtResource structures identifying the
resources which the widget understands:

typedef struct _XtResource {
    String	resource_name;	/* Resource name			    */
    String	resource_class;	/* Resource class			    */
    String	resource_type;	/* Representation type desired		    */
    Cardinal	resource_size;	/* Size in bytes of representation	    */
    Cardinal	resource_offset;/* Offset from base to put resource value   */
    String	default_type;	/* representation type of specified default */
    XtPointer	default_addr;	/* Address of default resource		    */
} XtResource, *XtResourceList;

The resource_size and resource_offset fields would be initialised by
applying sizeof() and offsetof() [1] to the field within the widget
instance structure which corresponds to the resource.

[1] Usually XtOffsetOf(); Xt predates C99 or the widespread
availablility of offsetof().

This allows the creation of generic functions (not tied to a specific
widget class) to set/get resource values for a particular widget
instance by name. IOW, roll-your-own OO with reflection.

-- 
Glynn Clements <glynn@gclements.plus.com>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2005-08-19  9:36 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-08-18 14:21 any compelling reasons to use "offsetof"? Robert P. J. Day
2005-08-19  5:46 ` _z33
2005-08-19  9:36 ` Glynn Clements

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).