Thursday, 28 April 2016

Opaque pointer and opaque data type

No comments:

Opaque data type:

When concrete implementation is not exposed to the user then it is called as opaque data type.

What is concrete implementation?

Consider you were writing one library and exposing to the user. Let the library be “Calculator”.  So when user wants to use your library one has to access from their source code.
Consider the following sample code,

int main()
CALCULATOR *l_calculator_object; // Opaque Data type
openCalculator(l_calculator_object); // Library function call
closeCalculator(l_calculator_object); // Library function call

Here library haven’t exposed the implementation of Calculator class. Implementation of Calculator class is present in the library. Here Calculator is the opaque data type.

At library, they may define the CALCULATOR as follows

typedef  calculator CALCULATOR;

So what is opaque pointer then?

Pointer to the opaque data type is called as an opaque pointer
Here l_calculator_object is an opaque pointer

opaque pointer:

Pointer variable defined with opaque data type is called opaque pointers.

Consider another example,

typedef struct s s_t;

Here Implementation details of struct s is hidden from the end users.
The end user can just declare and use the struct for further communication to the functionality module
FILE pointer in c is also one of the opaque pointer.

Eg: FILE *fp;
fclose(fp); // Use of opaque pointer at client side
Objective of the concept

To hide the implementation details of the data structure.


Changes in the type will not affect the client code (For Eg if we add one more member in the structure it will not affect the client code. so client code no need to be compiled again. Only the library has to be compiled.

Information hiding

Information hiding is nothing but hiding the implementation details to the end user. Here opaque pointer or opaque data type is one of the information hiding.
Why opaque data type is one of the information hiding technique?
Consider the FILE pointer in c file library. Here we know FILE is an structure or type definition of some structure.

FILE *l_file_pointer;

Here we don’t know the members which is defined inside that structure, it means they hidden information inside that structure. So it is called as information hiding.

Transparent data type

Transparent data type is opposite to the opaque data type. Here information about the structure like member of the structure, data type of the member will be open to the programmer.
Consider the following code.

struct myStruct
int a;
int main()
myStruct l_obj;
Here myStruct is an transparent data type.

Declaring local object for the opaque data type

You cannot declare the object for the opaque data type. This is because, when local object is created for some structure, then during compilation compiler will expect for the concrete implementation of the structure. Since concrete implementation for the structure is not available, you cannot create an object for the opaque data type.

Calculating the size of opaque data type

No, you cannot calculate the size of the opaque data type.

Why you cannot calculate the size of the opaque data type?
Since you don’t have the concrete implementation of the structure, you cannot create the object to that structure. So obviously you cannot calculate the size of the opaque data type.

Advantages of opaque pointer

1.       We can hide the implementation of the class to the user of the class
2.       Any change the structure of the class will not affect the user of the class because client is using only an opaque pointer
3.       Whenever class structure is changed, user of the class no needs to change the library.

Disadvantages of opaque pointer

Class cannot be accessed directly. Everything needs to be accessed as member function.

Application of opaque pointer:

Opaque data type is mainly used in the operating system related activity like writing content in file, reading a content of file etc.,

No comments:

Post a Comment