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
4
votes
1 answer

Typesafe StablePtrs

I spent a lot of time encoding invariants in my data types and now I am working on exposing my library to C via the FFI. Rather than marshal data structures across the language barrier I simply use opaque pointers to allow C to build up an AST and…
Brandon Ogle
  • 715
  • 1
  • 8
  • 23
4
votes
1 answer

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

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…
crisls
  • 43
  • 5
4
votes
3 answers

Creating an opaque pointer (reference) in C#

I have a class (basically a linked list, so let's call it List) that uses another class to store data (the nodes, so let's call the class Node). Node has methods that must be called from List, but calling them from elsewhere could be messy. I need…
Tomeamis
  • 477
  • 6
  • 14
4
votes
2 answers

c++11 class member array size constexpr forward declaration

I want to exclude some headers from my include chain after having used them. From what I know there is no exclude "header.h" in c++11. Pseudo Code Wishful thinking: #include "the_bad_header.h" //long includechain with later unused declarations class…
3
votes
2 answers

No-copy type annotation

In C, if a structure contains a futex or for whatever reason doesn't make sense to copy or move to a new address, is there any way (type annotation or something) to restrict/warn users from accidentally making copies of those objects?
user100046
  • 422
  • 4
  • 11
3
votes
3 answers

C struct information hiding (Opaque pointer)

I'm currently a bit confused regarding the concept of information hiding of C-structs. The backround of this question is an embedded c project with nearly zero knowledge of OOP. Up until now I always declared my typedef structs inside the header…
Evox402
  • 111
  • 1
  • 13
3
votes
2 answers

In this case, how to modularize program as well as achieving information hiding?

I created two classes "DEVICE_s" and "DEVICE_SET_s" as following: Device_Manager.h typedef struct DEVICE_s DEVICE_s; typedef struct DEVICE_SET_s DEVICE_SET_s; Device_Manager.c struct DEVICE_s { uint32_t IP; TYPE_e Type; METHOD_e…
Andy Lin
  • 397
  • 1
  • 2
  • 21
3
votes
1 answer

Using an opaque pointer to a non struct type

In my C programming, I use opaque-pointers to struct as a way to enforce abstraction and encapsulation of my code, in that manner : interface_header.h: typedef struct s_mytype t_mytype; defs_header.h: struct s_mytype { /* Actual definition of the…
VannTen
  • 441
  • 2
  • 12
3
votes
1 answer

ABAddressBook fromOpaque(_:) in Swift 3

So I've just updated to Xcode 8 and converted my Swift 2.3 code to Swift 3, and I have an error in this line of code that wasn't in Swift 2.3: func populateFrom(_ addressBook:ABAddressBook) { let allPeople =…
Charlie Pico
  • 2,036
  • 2
  • 12
  • 18
3
votes
1 answer

Opaque structures with multiple definitions

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…
Jubatian
  • 2,171
  • 16
  • 22
3
votes
2 answers

C/C++ opaque pointer library

Is there library/header already written to manage C++ objects from C using opaque pointers/handles? I can write one myself, but I would rather use already made solution, especially if it has fortran bindings. my specific requirements are: wrapper…
Anycorn
  • 50,217
  • 42
  • 167
  • 261
3
votes
2 answers

C++ hiding inherited class?

I am trying to hide the inclusion of a third party file in a main class header in a library I wrote, from the executables that link it. What I mean is: I have a library that I wrote that defines class A. Class A inherits from class B (which is…
latreides
  • 379
  • 1
  • 4
  • 14
3
votes
0 answers

What are the pros and cons for opaque pointers vs id numbers using the C programming language?

I'm currently using opaque pointers as my standard technique for encapsulation, but looking at the OpenGL API makes me think that using id numbers could be a better choice. I would like some advice from seasoned C programmers (I've only been using…
arkod
  • 1,973
  • 1
  • 20
  • 20
3
votes
1 answer

Opaque data type in C

What is the preferred way in C to return an opaque data type? /* Option #1: */ struct widget; struct widget *foo(); /* Option #2: */ struct widget { struct widget_impl *impl; }; struct widget foo(); Are there any other options which are more…
Simple
  • 13,992
  • 2
  • 47
  • 47
2
votes
1 answer

How does linking work in C with regards to opaque pointers?

So, I've been having a bit of confusion regarding linking of various things. For this question I'm going to focus on opaque pointers. I'll illustrate my confusion with an example. Let's say I have these three files: main.c #include…
thepufferfish
  • 441
  • 4
  • 17