Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an opaque pointer in C?

May I know the usage and logic behind the opaque pointer concept in C?

like image 633
Renjith G Avatar asked Sep 26 '11 10:09

Renjith G


People also ask

What does Opaque mean in C?

Opaque pointer is a pointer which points to a data structure whose contents are not exposed at the time of its definition. Example: struct STest* pSTest; It is safe to assign NULL to an opaque pointer.

What is an opaque object handle?

Opaque handles are used to refer to an object, but are themselves not an object. They are often implemented in terms of pointers to a type that user code knows nothing about or a wrapped integer and can only be used by passing the handle to library-provided functions.

What is an opaque string?

What are opaque strings? By "opaque" they mean that the inner structure exists, but is unknown. So the program is expected to treat the string as a whole – store it, transmit it, but not try to interpret.30-Apr-2011.

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.


1 Answers

An opaque pointer is one in which no details are revealed of the underlying data (from a dictionary definition: opaque: adjective; not able to be seen through; not transparent).

For example, you may declare in a header file (this is from some of my actual code):

typedef struct pmpi_s *pmpi; 

which declares a type pmpi which is a pointer to the opaque structure struct pmpi_s, hence anything you declare as pmpi will be an opaque pointer.

Users of that declaration can freely write code like:

pmpi xyzzy = NULL; 

without knowing the actual "definition" of the structure.

Then, in the code that knows about the definition (ie, the code providing the functionality for pmpi handling, you can "define" the structure:

struct pmpi_s {     uint16_t *data;     // a pointer to the actual data array of uint16_t.     size_t sz;          // the allocated size of data.     size_t used;        // number of segments of data in use.     int sign;           // the sign of the number (-1, 0, 1). }; 

and easily access the individual fields of it, something that users of the header file cannot do.

More information can be found on the Wikipedia page for opaque pointers..

The main use of it is to hide implementation details from users of your library. Encapsulation (despite what the C++ crowd will tell you) has been around for a long time :-)

You want to publish just enough details on your library for users to effectively make use of it, and no more. Publishing more gives users details that they may come to rely upon (such as the fact the size variable sz is at a specific location in the structure, which may lead them to bypass your controls and manipulate it directly.

Then you'll find your customers complaining bitterly when you change the internals. Without that structure information, your API is limited only to what you provide and your freedom of action regarding the internals is maintained.

like image 52
paxdiablo Avatar answered Sep 30 '22 21:09

paxdiablo