From: Glynn Clements <glynn@gclements.plus.com>
To: "Robert P. J. Day" <rpjday@mindspring.com>
Cc: C programming list <linux-c-programming@vger.kernel.org>
Subject: Re: curious about whether i can count on certain features of C
Date: Mon, 30 May 2005 14:24:44 +0100 [thread overview]
Message-ID: <17051.5148.70041.231276@gargle.gargle.HOWL> (raw)
In-Reply-To: <Pine.LNX.4.61.0505291517130.31836@localhost.localdomain>
Robert P. J. Day wrote:
> i just inherited a sizable C-based project and, perusing the code,
> i've come across a number of in-house defined constructs that would
> seem to already be supported in standard (C99?) C, and i'm wondering
> if there's a reason the previous developer felt he needed to define
> these features himself.
>
> first, there's the definition of "offsetof":
>
> #ifndef offsetof
> # define offsetof(type, field) \
> ( (char *) &( ((type *) 0)[0].field ) - (char *) &( ((type *) 0)[0] ) )
> #endif
>
> using any modern definition of C, can i safely assume that this
> function/macro is just part of the language (stddef.h)?
If you require C99, you can rely upon the presence of offsetof.
However, I wouldn't make code C99-only for this reason alone. Reasons
for not requiring C99 include:
1. You might want to compile your code on a system where there isn't a
stable C99 compiler.
2. You might want to use a third-party library whose headers aren't
compatible with C99 (C99 isn't entirely backward-compatible with C89).
> and, at the
> very least, is there a reason it's defined in such an obscure way
> rather than just
>
> #define offsetof(type,memb) ((size_t)&((type *)0) -> memb) ???
>
> that first definition might be technically correct but i'm really
> trying to simplify things and i don't see any obvious need to keep
> that local definition around.
1. Some compilers may complain about an explicit dereference of a null
pointer.
2. On some systems, casting the base address of the structure to
size_t may not yield zero.
> next, booleans. based on my copy of "harbison and steele" (5th
> ed.), can i reasonably assume the existence of a boolean data type
> (stdbool.h)? i don't have much interest in supporting legacy
> compilers, and booleans appear to be part of the C99 definition, so
> i'd be really tempted to ditch the following enum type i found:
>
> enum TCS_bool_Type {
> TCS_bool_FALSE = 0, /**< false/no state */
> TCS_bool_TRUE /**< true/yes state */
> } GCC_PACKED; /* enum TCS_bool_Type */
>
> next, there are a number of typedefs for fixed-width data types:
>
> typedef signed char TCS_int8_t; /**< signed 8-bit integer */
> typedef unsigned char TCS_u_int8_t; /**< unsigned 8-bit integer */
> typedef signed short TCS_int16_t; /**< signed 16-bit integer */
> typedef unsigned short TCS_u_int16_t; /**< unsigned 16-bit integer */
> typedef signed int TCS_int32_t; /**< signed 32-bit integer */
> typedef unsigned int TCS_u_int32_t; /**< unsigned 32-bit integer */
> typedef TCS_int8_t TCS_tiny_t; /**< signed tiny integer */
> typedef TCS_u_int8_t TCS_u_tiny_t; /**< unsigned tiny integer
>
> is there any compelling reason why i can't just use the types defined
> in /usr/include/stdint.h? that is, int8_t, uint32_t, and so on? is
> there any rationale for someone wanting to do this themselves, apart
> from perhaps legacy compiler support?
Just portability. In the real world, not everyone uses a bleeding-edge
compiler.
--
Glynn Clements <glynn@gclements.plus.com>
prev parent reply other threads:[~2005-05-30 13:24 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-05-29 19:31 curious about whether i can count on certain features of C Robert P. J. Day
2005-05-30 13:24 ` Glynn Clements [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=17051.5148.70041.231276@gargle.gargle.HOWL \
--to=glynn@gclements.plus.com \
--cc=linux-c-programming@vger.kernel.org \
--cc=rpjday@mindspring.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).