Path: csiph.com!news.swapon.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: configure() -- a new C compiler preprocessor and code injection proposal Date: Tue, 01 Sep 2015 08:12:35 -0700 Organization: None to speak of Lines: 39 Message-ID: References: <93b0ff7b-6ee2-48f9-aad4-6e3b91e9ceac@googlegroups.com> <378d561b-d273-423b-aedc-9e26bcc25102@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="10922"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/48vib6RWfkhaHOS2mCUeo" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:gNI41Zsx9SMVWOqdKZLdnyOFhHs= sha1:QUypIv8ccm0358Kb0DQt91r9EI8= Xref: csiph.com comp.lang.c:68866 David Brown writes: [...] >> On Monday, August 31, 2015 at 2:22:12 PM UTC-4, Keith Thompson wrote: >>> How would you implement offsetof() without using a macro? >>> >>> offsetof (defined in ) takes two arguments, a type name and a >>> member name. For example: >>> >>> struct foo { >>> int x; >>> int y; >>> }; >>> >>> offsetof(struct foo, x) == 0 >>> offsetof(struct foo, y) == sizeof (int) (most likely) [...] > You would need casts to integer types here: > > int lnOffsetX = ((uintptr_t) &foo.x - (uintptr_t) &foo); No, that would not work portably. It assumes that uintptr_t exists, which is not guaranteed (there might not be any unsigned integer type wide enough to hold a converted pointer value without loss of information). And it assumes that arithmetic on uintptr_t corresponds to arithmetic on pointers, which also is not guaranteed -- and I've worked on systems where it doesn't. Casting to char rather than to uintptr_t would work, except that ... > Of course, that requires the existence of an object "foo" of the struct > type - while offsetoff works /without/ having any known object of that type. [...] -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"