In computer programming, an opaque pointer is a special case of an opaque data type, a data type declared to be a pointer to a record or data structure of some unspecified type.
Opaque pointers are present in several programming languages including Ada, C, C++, D and Modula-2.
If the language is strongly typed, programs and procedures that have ...
#What is an opaque pointer and why is it useful?
12 messages · Page 1 of 1 (latest)
Like, you have a library, the user doesn't need to see exactly what the data type is, then you have an opaque pointer.
a void* is often opaque
where I have seen it used as an example very often is in callbacks where the original caller puts some void* that has some information related to the request it's making, then the fulfiller holds onto this pointer and calls back the requester and gives that void*
so then the requester upon getting the callback knows which thing it's getting a callback for
another example is library encapsulation like maybe there is some library that has a very complex data structure as a core object in the library so when you ask for one it gives you it as a void* (typedef to some other name usually) then when you use functions from that library you provide an object like that plus some other parameters that tell the library what to do with that data
malloc even is the most simple thing you can use that gives an opaque pointer
malloc, free, calloc, realloc, reallocarray - allocate and free dynamic memory
Synopsis
#include <stdlib.h>
void *malloc(size_t size);
void free(void *ptr);
void *calloc(size_t nmemb, size_t size);
void *realloc(void *ptr, size_t size);
void *reallocarray(void *ptr, size_t nmemb, size_t size);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
reallocarray():
Since glibc 2.29:
_DEFAULT_SOURCE
... (truncated)
it just returns a pointer to a region of bytes for you to use, and you can cast it to whatever you want
this isn't a perfect example of an opaque pointer in the strictest sense especially from a caller perspective but it gives sort of the idea
!solved
Thank you and let us know if you have any more questions!
This thread is now set to auto-hide after an hour of inactivity