opaque-pointers

Why can't I create an opaque data type?

时光毁灭记忆、已成空白 提交于 2021-02-10 15:56:17
问题 I'm trying to experiment with opaque data types to get an understanding of them. The main problem is that I keep getting an 'incomplete' error. main.c #include <stdio.h> #include <stdlib.h> #include "blepz.h" int main() { setfnarp(GOO,5); int loogaboo = getfnarp(GOO); printf("%i", loogaboo); return 0; } fnarpishnoop.c #include "blepz.h" struct noobza { int fnarp; }; void setfnarp(struct noobza x, int i){ x.fnarp = i; }; int getfnarp(struct noobza x){ return x.fnarp; }; blepz.h struct noobza;

Why can't I create an opaque data type?

為{幸葍}努か 提交于 2021-02-10 15:55:02
问题 I'm trying to experiment with opaque data types to get an understanding of them. The main problem is that I keep getting an 'incomplete' error. main.c #include <stdio.h> #include <stdlib.h> #include "blepz.h" int main() { setfnarp(GOO,5); int loogaboo = getfnarp(GOO); printf("%i", loogaboo); return 0; } fnarpishnoop.c #include "blepz.h" struct noobza { int fnarp; }; void setfnarp(struct noobza x, int i){ x.fnarp = i; }; int getfnarp(struct noobza x){ return x.fnarp; }; blepz.h struct noobza;

Opaque structures with multiple definitions

北城以北 提交于 2020-01-04 01:26:07
问题 I am considering realizing a simple interface pattern in the C language. A key characteristic is that it would provide multiple definitions for an opaque sturcture supplied by the interface's public header, that is, different implementations would provide different underlaying data for that structure (so across different translation units, the same structure would have different implementation). I couldn't find any reference whether this would be a good or bad design pattern. At least it

Opaque C structs: how should they be declared?

吃可爱长大的小学妹 提交于 2019-12-27 11:42:26
问题 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; }; 回答1: 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

How do I create a module in MISRAC:2012 that follows Dir 4.12 and 4.8?

浪尽此生 提交于 2019-12-10 18:51:58
问题 This question relates to coding in ISO C99 following the MISRAC:2012 guidelines. I am looking for guidance on Dir 4.8 “If a pointer to a structure or union is never dereferenced within a translation unit, then the implementation of the object should be hidden” in conjunction with Dir 4.12 “Dynamic memory allocation shall not be used”. When implementing an Abstract Data Type in C it is common to refer to the ADT using a handle that is a pointer to a structure describing the internal state of

Is casting from TYPE* to unsigned char* allowed?

久未见 提交于 2019-12-08 18:24:04
问题 C99 -- specifically section 6.2.6.1, paragraph 4 -- states that copying an object representation into an array of unsigned char is allowed: struct { int foo; double bar; } baz; unsigned char bytes[sizeof baz]; // Do things with the baz structure. memcpy(bytes, &baz, sizeof bytes); // Do things with the bytes array. My question: can we not avoid the extra memory allocation and copy operation by simply casting? For example: struct { int foo; double bar; } baz; unsigned char *bytes = (void *)

What does the term “opaque type” mean in the context of “CFBundleRef opaque type”?

会有一股神秘感。 提交于 2019-12-03 03:06:35
问题 Does someone have a good explanation of what an "opaque type" is? I saw that term in context of the CFBundleRef , where they were saying: "CFBundleRef opaque type". Is that a type that's readonly? 回答1: An "opaque type" is a type where you don't have a full definition for the struct or class . In C, C++ and Objective-C, you can tell the compiler that a type will be defined later by using a forward declaration: // forward declaration of struct in C, C++ and Objective-C struct Foo; // forward

Trouble using opaque pointers in Objective C++

橙三吉。 提交于 2019-12-02 15:23:48
问题 The answer to this quesion explains that opaque pointers are a good way to include C++ member variables in an Objective C++ header. I'm getting compile errors when trying to follow the example. Here's the relevant code from my header, with the corresponding compiler errors shown as comments: struct ADSR_opaque; // error: forward declaration of 'struct ADSR_opaque' @interface LoopyPulser : NSObject{ float _pulseRate; UInt32 tickInterval; UInt32 step; InMemoryAudioFile * audioFilePlayer; ADSR

What does the term “opaque type” mean in the context of “CFBundleRef opaque type”?

∥☆過路亽.° 提交于 2019-12-02 15:11:48
Does someone have a good explanation of what an "opaque type" is? I saw that term in context of the CFBundleRef , where they were saying: "CFBundleRef opaque type". Is that a type that's readonly? An "opaque type" is a type where you don't have a full definition for the struct or class . In C, C++ and Objective-C, you can tell the compiler that a type will be defined later by using a forward declaration: // forward declaration of struct in C, C++ and Objective-C struct Foo; // forward declaration of class in C++: class Bar; // forward declaration of class in Objective-C: @class Baz; The

c typedef(ed) opaque pointer

拥有回忆 提交于 2019-11-30 11:23:24
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? You already have the typedef in your header, so include that and define struct foo in the implementation without the typedef . foo.h : typedef struct foo foo; foo *create_foo(...); delete_foo(foo *f); foo.c : #include <foo.h>