Re: Typdef pointers to structs or not?
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Thu, 27 Jul 2006 20:02:51 GMT
Chris Dollin <chris.dollin@xxxxxx> writes:
Johan Tibell wrote:
Could someone outline the pros and cons of typedefing pointers to
structs like this?
typedef struct exp_ {
int val;
struct exp_ *child;
} *exp;
(This is straight from memory so it might not even compile.)
The use case is AST representation which makes heavy use of pointers to
structs (in fact IIRC it only uses pointers, never plain structs).
Right now I don't use typedefs since I read in the Linux kernel coding
guidelines that (the author believes) that it obfuscates what's really
going on. Note that pretty much every function in the compiler will
modify these structs so either the whole typedef will be in a .h file
or a bunch of access functions are needed.
Cons:
some C programmers don't like it
it's harder to tell whether something is a pointer or not
Pros:
easier to treat as a purely abstract type
For me, the (pro) is a no-brainer. Other programmers in other contexts
may reasonably feel differently.
In your particular circumstances -- expression trees -- I have in
fact "typedefed pointers", for exactly the abstract type reason:
I changed the representation later so that an Exp wasn't
a pointer (and, IIRC, changed it back later later). Having stray
*'s all over the place would have made that rather trickier.
[Access to the fields was in any case via functions-which-might-be-macros,
so -> vs . vs getWossname wasn't an issue.]
IMHO, typedefing a pointer makes sense if and only if it's really to
be treated as a purely abstract type. This means that if you're
writing code that uses it, you don't, and shouldn't, even know that
it's a pointer. You never dereference it or perform arithmetic on it,
except through functions provided as part of the abstract type's
interface.
In the example above, if client code, having declared
exp x;
will ever refer to x->val or x->child, or do "x = malloc(sizeof *x);"
or "free(x)", then the type should visibley be a pointer type.
If, on the other hand, the interface is restricted to things like
x = new_exp();
set_val(x, 42);
then it could make sense to hide the pointer-ness behind a typedef.
If the interface could be re-written to make exp a structure rather
than a pointer, with no required changes in the client code, then it's
purely abstract.
<OT>
Some languages encourage a different style. For example, in some
languages most or all declarations that create new types create a
single-identifier name for them. In such a language, I'd feel more
comfortable about hiding a type's nature behind a single identifier.
But in C, most code that uses a type depends intimately on the nature
of the type; completely hiding the nature of a type is more difficult,
and partially hiding it is likely to be futile.
</OT>
(Incidentally, I'd call it "expr" rather than "exp"; "exp" is a
function declared in <math.h>.)
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.
- References:
- Typdef pointers to structs or not?
- From: Johan Tibell
- Re: Typdef pointers to structs or not?
- From: Chris Dollin
- Typdef pointers to structs or not?
- Prev by Date: Re: Is this a good habit?
- Next by Date: Re: The C language in the planet Mars
- Previous by thread: Re: Typdef pointers to structs or not?
- Next by thread: Re: Typdef pointers to structs or not?
- Index(es):
Relevant Pages
|