What Is an Opaque Value in C++

What is an opaque value in C++?

An example for an Opaque Value is FILE (from the C library):

#include <stdio.h>

int main()
{
FILE * fh = fopen( "foo", "r" );
if ( fh != NULL )
{
fprintf( fh, "Hello" );
fclose( fh );
}
return 0;
}

You get a FILE pointer from fopen(), and use it as a parameter for other functions, but you never bother with what it actually points to.

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

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.

What is an opaque pointer in C?

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.

What do transparent and opaque mean when applied to programming concepts?

In the examples you give, transparent is being used to mean hidden in the sense of things taking place automatically behind the scenes (i.e. without the user of the code or the program having to interact).

Opaque is also being used to mean hidden, which is perhaps where the confusion comes in. The term opaque type has a specific meaning in C/C++, where it refers to a type that has been declared but not yet defined.

In both cases, I think people are using these terms to express a lack of visibility. Transparent is used where something is present, but you can't see it. Opaque is used where something is present, but you can't see inside it to inspect its inner workings.

Opaque C structs: various ways to declare them

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 foo *f);

// foo.c
struct foo {
int x;
int y;
};

If you really can't stand typing the struct keyword, typedef struct foo foo; (note: get rid of the useless and problematic underscore) is acceptable. But whatever you do, never use typedef to define names for pointer types. It hides the extremely important piece of information that variables of this type reference an object which could be modified whenever you pass them to functions, and it makes dealing with differently-qualified (for instance, const-qualified) versions of the pointer a major pain.

What is an opaque byte string?

They probably mean a byte array of unspecified format. 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.

The C++ example would be an instance of std::vector<unsigned char>. A C example would be an array of unsigned char (either dynamic AKA malloc'ated or static).

How do I use Opaque Pointers in C?

This line is wrong:

d_link_list_t* list = (d_link_list_t*)malloc(sizeof(d_link_list_t*));

It allocates the wrong amount of memory. You need to allocate the size of the thing being pointed to, not the size of the pointer.

However this is a bad idea anyway. The function should be:

d_link_list_t Initialize(void)
{
// Now make concrete list_t pointer and set the members
list_t * rlP;
rlP = malloc(sizeof(*rlP));
rlP->head = NULL;
rlP->tail = NULL;
rlP->count = 1;

return rlP;
}

Not sure what you were trying to do with list->count = 0;, because list has an opaque type, that is not possible. Also unclear why you write rlP->count = 1 instead of = 0.

If you want to use pointer syntax (i.e. d_link_list_t *) then you should remove the * from the typedef line.

How do I see inside opaque data types?

Unless you already have the source, there is no feasible way to "hack inside" an opaque type without having a clue of the underlying representation on a specific system. Hacking it would involve following each access to the opaque pointer in run-time and see where it goes, then start guessing from there.

Instead, you could Google for example the glibc source at Github. In stdio.h there is a conditional typedef which points either at __fpos_t or __fpos64_t in internal headers:

__fpos_t.h

typedef struct _G_fpos_t
{
__off_t __pos;
__mbstate_t __state;
} __fpos_t;

__fpos64_t.h

typedef struct _G_fpos64_t
{
__off64_t __pos;
__mbstate_t __state;
} __fpos64_t;

Not very exciting, but you can continue to trace those various types at Github from there and see what they boil down to in the end. Integers and enums, most likely.



Related Topics



Leave a reply



Submit