Re: Opaque pointers
websnarf@gmail. com wrote:
When I say a typedef "allows client code to know less"
what I mean is code can be written that doesn't depend
on the types representation. Not that it _can't_ be
written to depend on the representation, but that
it _can_ be written so that it _doesn't_ depend.
That's an important property, and using typedef
enables that.
That's true but irrelevant to the point I'm making.
>
If you were able to somehow *hide* this typedef, and have the typedef
just magically be there, and somehow turn off its compatibility with
other pointers and sizeof(), then I would agree with you. But you
don't have this.
>
In fact if you want to truly "opaquify" a pointer, the way to do that,
is to put it into an opaque struct (or union).
To be technically accurate, C doesn't have opaque types.
The standard doesn't mention them, and there is no construct
in C that produces a type that is 'opaque' in the sense
that the term is used in languages that have opaque types
incorporated into the language.
Rather, what C has is ways of making types that we
can _think of_ as opaque. Using typedef is one way
of doing this. Putting a struct definition in
a source file rather than a header file is another
way. Wrapping the representation inside a struct
and using only the struct in client code is another
way. Each of these ways may offer certain advantages
or disadvantages, but none of them creates a type
that is truly opaque, only a type that is convenient
to think of as opaque. That's why using typedef
is relevant to the notion of opaque types in C.
websnarf@gmail. com wrote:
ena8t8si@yahoo. com wrote:
>
No, the typedef has to be exposed in the header file (or possibly
somewhere else). But the typedef is just a pointer. This structure is
essentially exposed even if you don't "look" at the header file.
websnarf@gmail. com wrote:
Using a typedef allows client code to know less
about a type's representation.
chankl wrote:
Can anyone explain what's an opaque pointer and how it's implemented in
C?
>
The other answers given are failrly reasonable. However, I would just
like to point out that opacity is generally focused on the undetailed
struct, and not the fact that it has been typedefed to a pointer.
I.e., the typedef struct List_T * List_T line is redundant and
unnecessary (and also going to cause C++ compilers to go into fits.)
You can simply use the expression struct List_T * instead of List_T
with no ill effect.
>
I.e., the pointers are derivative of the overall idea of making opaque
structs, and the additional typedef is not relevant to the opacity.
Can anyone explain what's an opaque pointer and how it's implemented in
C?
>
The other answers given are failrly reasonable. However, I would just
like to point out that opacity is generally focused on the undetailed
struct, and not the fact that it has been typedefed to a pointer.
I.e., the typedef struct List_T * List_T line is redundant and
unnecessary (and also going to cause C++ compilers to go into fits.)
You can simply use the expression struct List_T * instead of List_T
with no ill effect.
>
I.e., the pointers are derivative of the overall idea of making opaque
structs, and the additional typedef is not relevant to the opacity.
about a type's representation.
No, the typedef has to be exposed in the header file (or possibly
somewhere else). But the typedef is just a pointer. This structure is
essentially exposed even if you don't "look" at the header file.
what I mean is code can be written that doesn't depend
on the types representation. Not that it _can't_ be
written to depend on the representation, but that
it _can_ be written so that it _doesn't_ depend.
That's an important property, and using typedef
enables that.
For example, you can say List_t x; /*... */ void * p = x; and the
compiler doesn't complain -- so this is a kind of exposure. Or more
minimally you can say sizeof (x) without trouble. Compare this with
struct List_t *x, *y, and try to do this: *x = *y, or sizeof(*x) or
sizeof(struct List_T). They both lead to compiler errors.
compiler doesn't complain -- so this is a kind of exposure. Or more
minimally you can say sizeof (x) without trouble. Compare this with
struct List_t *x, *y, and try to do this: *x = *y, or sizeof(*x) or
sizeof(struct List_T). They both lead to compiler errors.
[...] Thus it is
relevant to how opaque the type is. Using a
typedef might be desirable or it might not
be, but it is relevant.
relevant to how opaque the type is. Using a
typedef might be desirable or it might not
be, but it is relevant.
If you were able to somehow *hide* this typedef, and have the typedef
just magically be there, and somehow turn off its compatibility with
other pointers and sizeof(), then I would agree with you. But you
don't have this.
>
In fact if you want to truly "opaquify" a pointer, the way to do that,
is to put it into an opaque struct (or union).
The standard doesn't mention them, and there is no construct
in C that produces a type that is 'opaque' in the sense
that the term is used in languages that have opaque types
incorporated into the language.
Rather, what C has is ways of making types that we
can _think of_ as opaque. Using typedef is one way
of doing this. Putting a struct definition in
a source file rather than a header file is another
way. Wrapping the representation inside a struct
and using only the struct in client code is another
way. Each of these ways may offer certain advantages
or disadvantages, but none of them creates a type
that is truly opaque, only a type that is convenient
to think of as opaque. That's why using typedef
is relevant to the notion of opaque types in C.
Comment