Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What defines an opaque type in C, and when are they necessary and/or useful?

Tags:

c

types

I've seen the concept of 'opaque types' thrown around a bit but I really haven't found a succinct answer as to what defines an opaque type in C and more importantly what problems they allow us to solve with their existence. Thanks

like image 750
SiegeX Avatar asked Feb 20 '10 07:02

SiegeX


People also ask

What is an opaque type in C?

In computer science, an opaque data type is a data type whose concrete data structure is not defined in an interface. This enforces information hiding, since its values can only be manipulated by calling subroutines that have access to the missing information.

What is an opaque object handle in C?

Opaque pointer is a pointer which points to a data structure whose contents are not exposed at the time of its definition.

What is an opaque object handle?

An opaque object and its handle are significant only at the process where the object was created, and cannot be transferred to another process. MPI provides certain predefined opaque objects and predefined, static handles to these objects. Such objects may not be destroyed.

What does opaque value mean?

"Opaque" is defined, in English, as "not able to be seen through; not transparent". In Computer Science, this means a value which reveals no details other then the type of the value itself.


2 Answers

It is the most generally used for library purpose. The main principe behind Opaque type in c is to use data though its pointer in order to hide data handling implementation. Since the implementation is hidden, you can modify the library without recompiling any program which depend on it (if the interface is respected)

eg: version 1:

// header file struct s;  int s_init(struct s **x); int s_f(struct s *x); int s_g(struct s *x);  // source file struct s { int x; }  int s_init(struct s **x) { *x = malloc(...); } int s_f(..) { ... } int s_g(..) { ... } 

version 2

// header file struct s;  int s_init(struct s **x); int s_f(struct s *x); int s_g(struct s *x);  // source file struct s { int y; int x; }  int s_init(struct s **x) { *x = malloc(...); } int s_f(..) { ... } int s_g(..) { ... } 

From your program side, nothing changed! and as said previously, no need to recompile every single program which rely on it.

like image 129
Phong Avatar answered Oct 15 '22 12:10

Phong


In my understanding, opaque types are those which allow you to hold a handle (i.e., a pointer) to an structure, but not modify or view its contents directly (if you are allowed to at all, you do so through helper functions which understand the internal structure).

Opaque types are, in part, a way to make C more object-oriented. They allow encapsulation, so that the internal details of a type can change--or be implemented differently in different platforms/situations--without the code that uses it having to change.

like image 41
Tim Yates Avatar answered Oct 15 '22 11:10

Tim Yates