Questions tagged [opaque-pointers]

In computer programming, an opaque pointer is a special case of an opaque data type, a datatype declared to be a pointer to a record or data structure of some unspecified type.

Opaque pointers are a way to hide the implementation details of an interface from ordinary clients, so that the implementation may be changed without the need to recompile the modules using it. This benefits the programmer as well since a simple interface can be created, and most details can be hidden in another file. This is important for providing binary code compatibility through different versions of a shared library, for example.

86 questions
101
votes
3 answers

What is an opaque pointer in C?

May I know the usage and logic behind the opaque pointer concept in C?
Renjith G
  • 4,718
  • 14
  • 42
  • 56
61
votes
4 answers

Opaque C structs: various ways to declare them

I've seen both of the following two styles of declaring opaque types in C APIs. What are the various ways to declare opaque structs/pointers in C? Is there any clear advantage to using one style over the other? Option 1 // foo.h typedef struct foo *…
splicer
  • 5,344
  • 4
  • 42
  • 47
51
votes
10 answers

Static allocation of opaque data types

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…
Bart
  • 1,633
  • 14
  • 21
31
votes
3 answers

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

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?
Thanks
  • 40,109
  • 71
  • 208
  • 322
19
votes
1 answer

c typedef(ed) opaque pointer

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…
Manish
  • 1,985
  • 2
  • 20
  • 29
13
votes
3 answers

Opaque types allocatable on stack in C

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…
Cyan
  • 13,248
  • 8
  • 43
  • 78
10
votes
1 answer

'init()' is deprecated: init() will be removed in Swift 3. Use `nil`

I was using this code. var audioUnit:AudioUnit = AudioUnit() But in Xcode 7.3 (Swift 2.2) I am getting this warning. Any idea why? And how can I get rid of that? N.B. Is I use nil then my program crashes.
user6297291
7
votes
1 answer

Is casting from TYPE* to unsigned char* allowed?

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…
user539810
6
votes
3 answers

Opaque struct without definition

I am designing an iterator interface for my hashmap data structure. The current design looks like this: // map.h typedef struct map_iterator map_iterator; // map.c struct map_iterator { // Implementation details }; // client.c map *m =…
Andrew Sun
  • 4,101
  • 6
  • 36
  • 53
6
votes
2 answers

unique_ptr to an opaque struct? (C++)

A library defines an opaque data type: struct OpaqueStruct; and the client code has to obtain and release an OpaqueStruct*. I have access to the library source. Unfortunately, neither shared_ptr nor unique_ptr cannot store that pointer giving…
18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
6
votes
1 answer

Move of class with pimpl won't compile

In the following example, how is it possible that ~CImpl is called correctly but when the class needs to be moved, the compiler says it has an incomplete type? If the declaration of Impl is moved to the header it works, my question is how come the…
piotr
  • 5,657
  • 1
  • 35
  • 60
6
votes
1 answer

CGColor internals

I hope to understand internals of CoreFoundation CGColor object with this research. I could find a sample definition of CGColor structure from free quartz project which seems to match the IOS declaration(relying on my researchs). typedef struct…
lockedscope
  • 965
  • 17
  • 45
5
votes
1 answer

Adding const-ness to opaque handle

If I have created a C module that presents a handle to the user with a pointer to a forward declared struct, like so: typedef struct FOO_Obj *FOO_Handle; If I then declare function prototypes that use it as a const qualified parameter thusly: void…
Toby
  • 9,696
  • 16
  • 68
  • 132
4
votes
3 answers

C opaque pointer gotchas

I'm working with a legacy C library interface (to C++) that exposes opaque pointers as typedef void * OpaqueObject In the library: OpaqueObject CreateObject() { return new OurCppLibrary::Object(); } This of course provides absolutely no type…
Cat Zimmermann
  • 1,422
  • 2
  • 21
  • 38
4
votes
1 answer

Dealing with opaque pointers in pybind11

I am moving a Python module written in C++ from Boost.Python to Pybind11. My C++ code relies on a C library that has opaque pointers. With Boost.Python, I used the documentation here to deal with these opaque pointers:…
sunmat
  • 6,976
  • 3
  • 28
  • 44
1
2 3 4 5 6