opaque-pointers

c typedef(ed) opaque pointer

允我心安 提交于 2019-11-29 17:04:14
问题 I've defined an opaque structure and related APIs like this: typedef struct foo foo; foo *create_foo(...); delete_foo(foo *f); I am not able to define the structure in my c file. Gives redefinition error. typedef struct foo { int implementation; }foo; I am able to use foo in c file without typedef but I want the typedef (i.e. use it directly as foo*). Is there a way? 回答1: You already have the typedef in your header, so include that and define struct foo in the implementation without the

Opaque types allocatable on stack in C

不羁岁月 提交于 2019-11-29 04:02:43
When designing a C interface, it is common to let into the public interface ( .h ) only what needs to be known by the user program. Hence for example, the inner components of structures should remain hidden if the user program does not need to know them. This is indeed good practice, as the content and behavior of the struct could change in the future, without affecting the interface. A great way to achieve that objective is to use incomplete types. typedef struct foo opaqueType; Now an interface using only pointers to opaqueType can be built, without the user program ever needing to know the

Am I correct to assume one cannot forward-declare a library's opaque pointer type?

风流意气都作罢 提交于 2019-11-28 14:22:26
There are a lot of questions out there about forward declarations and opaque types, but most seem to be from the perspective of the library author, or people trying to use incomplete types without pointers or some such. I'm using a library whose interface accepts/returns FOO * pointers. I'd like to confirm that I cannot (or should not) somehow forward-declare FOO or FOO * in my header file (which defines a struct with a FOO * member). I do know that I could just #include <library.h> in both my header and my .c file, but since this is really just a learning project, I wanted to get

Opaque types allocatable on stack in C

不羁的心 提交于 2019-11-27 17:52:15
问题 When designing a C interface, it is common to let into the public interface ( .h ) only what needs to be known by the user program. Hence for example, the inner components of structures should remain hidden if the user program does not need to know them. This is indeed good practice, as the content and behavior of the struct could change in the future, without affecting the interface. A great way to achieve that objective is to use incomplete types. typedef struct foo opaqueType; Now an

Am I correct to assume one cannot forward-declare a library's opaque pointer type?

独自空忆成欢 提交于 2019-11-27 08:34:04
问题 There are a lot of questions out there about forward declarations and opaque types, but most seem to be from the perspective of the library author, or people trying to use incomplete types without pointers or some such. I'm using a library whose interface accepts/returns FOO * pointers. I'd like to confirm that I cannot (or should not) somehow forward-declare FOO or FOO * in my header file (which defines a struct with a FOO * member). I do know that I could just #include <library.h> in both

Static allocation of opaque data types

亡梦爱人 提交于 2019-11-27 03:15:54
Very often malloc() is absolutely not allowed when programming for embedded systems. Most of the time I'm pretty able to deal with this, but one thing irritates me: it keeps me from using so called 'opaque types' to enable data hiding. Normally I'd do something like this: // In file module.h typedef struct handle_t handle_t; handle_t *create_handle(); void operation_on_handle(handle_t *handle, int an_argument); void another_operation_on_handle(handle_t *handle, char etcetera); void close_handle(handle_t *handle); // In file module.c struct handle_t { int foo; void *something; int another

Opaque C structs: how should they be declared?

无人久伴 提交于 2019-11-26 18:40:39
I've seen both of the following two styles of declaring opaque types in C APIs. Is there any clear advantage to using one style over the other? Option 1 // foo.h typedef struct foo * fooRef; void doStuff(fooRef f); // foo.c struct foo { int x; int y; }; Option 2 // foo.h typedef struct _foo foo; void doStuff(foo *f); // foo.c struct _foo { int x; int y; }; My vote is for the third option that mouviciel posted then deleted: I have seen a third way: // foo.h struct foo; void doStuff(struct foo *f); // foo.c struct foo { int x; int y; }; If you really can't stand typing the struct keyword,

Static allocation of opaque data types

送分小仙女□ 提交于 2019-11-26 10:28:03
问题 Very often malloc() is absolutely not allowed when programming for embedded systems. Most of the time I\'m pretty able to deal with this, but one thing irritates me: it keeps me from using so called \'opaque types\' to enable data hiding. Normally I\'d do something like this: // In file module.h typedef struct handle_t handle_t; handle_t *create_handle(); void operation_on_handle(handle_t *handle, int an_argument); void another_operation_on_handle(handle_t *handle, char etcetera); void close