Table of contents
1.
Introduction
2.
Opaque Pointer
2.1.
Syntax
2.2.
Need for Opaque Pointer
3.
Frequently Asked Questions
3.1.
What is Struct?
3.2.
When should we use Opaque Pointers?
3.3.
Is there any disadvantage to using an Opaque Pointer?
4.
Conclusion
Last Updated: Mar 27, 2024
Medium

Opaque Pointer

Author SHIVAM SINHA
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A pointer is a variable whose value is the address of another variable, i.e., the memory location's direct address. Pointers help in saving memory space and help in accessing memory efficiently.

An opaque pointer is a pointer whose contents are abstracted at the time of definition. It is one of the powerful forms of abstraction for use in libraries. We will see how we can use the Opaque pointer.

Also see, Literals in C, Fibonacci Series in C++

Opaque Pointer

Opaque Pointers are made up of two words, opaque and pointers. Opaque means something which is not transparent, it means we cannot see through it, and the pointers are simply a variable that holds the address of another variable. Opaque Pointers is one of the powerful features in C and C++, which can be used just like abstraction as its contents are kept hidden at the time of its definition.

Now let's see how we can declare an opaque pointer.

Syntax

Struct STest* pStest; 

So here, it is not possible to say which type of data STest structure is going to store by looking only at its definition.

It is safe for the opaque pointer to assign a NULL value.

pSTest= NULL;

Need for Opaque Pointer

Now, the question arises here: why do we need to use opaque pointers? So, there are places where we don't know the implementation, and we want the compiler to ignore it because it will be implemented by our clients while preparing the compilation unit.

Let's understand this by considering one situation:

Suppose we are creating an app that will deal with images; now we know that different people use different platforms, so we have to take care of all the platforms like windows, android, and apple. We have to share the code used by all the platforms, and then different end-points can have platform-specific code having a good design, which means that it should be scalable, robust, and flexible as per our requirements.

So we have a class named PImage, to deal with images, which exposes APIs to deal with image operations like scale, rotate, move, save, etc.

 All the platforms we mentioned, like windows, android, and apple, have the same operations, so we need to define this class in a header file. But the important thing, the way an image is handled might differ across different platforms. Like windows can have a different mechanism to access pixels of an image than apple does. So to solve this problem and work on shared code, we would be doing something like this.

 

Image.h: A header file that will store class declaration.

// class PImage provides an API that deals with various operations and different platforms can implement these operations in // different ways.

class PImage
{
public:
    PImage();
    ~PImage();
    struct ShowImageInfo* pixelImageInfo;
    void Move(int toX, int toY);    
    void Scale(double scaleFactorX, 
               double scaleFactorY);
 void Rotate(double angle);
   
private:
    void ImageInfo();
};

 

Image. cpp: This is the code that will be distributed among different end-points  

PImage::PImage()
{
    ImageInfo();
}
  
PImage::~PImage()
{
    
}

 

Image_windows.cpp : Code specific to Windows will reside here

struct ShowImageInfo
{
   // Windows specific DataSet
};
  
void PImage::ImageInfo()
{
    pixelImageInfo = new ShowImageInfo;
    // Initialize windows specific info here
}
  
void PImage::Rotate()
{
    // Make use of windows specific ShowImageInfo
}

 

Image_apple.cpp: Code specific to Apple will reside here

struct ShowImageInfo
{
    // specific for apple
};
void PImage::ImageInfo()
{
    pixelImageInfo = new ShowImageInfo;
      
    // Initialize apple specific info here
}
void PImage::Rotate()
{
    // Make use of apple specific ShowImageInfo
}

 

Now, we can clearly see from the above example that we have defined the PImage class's blueprint. We mention that there is a ShowImageInfo data structure, but the contents of ShowImageInfo are unknown. Now defining this data structure is the clients' responsibility, i.e., windows, apple, and android, and they can use it as per their requirements. Meanwhile, if we want to develop an app for a new end-point, 'X,' the design is already there. We only need to define ShowImageInfo for the end-point 'X' and use it accordingly.

Frequently Asked Questions

What is Struct?

A struct is a keyword that is used to create a user-defined data type in C or C++.

When should we use Opaque Pointers?

We should use opaque pointers when we need multiple instances, and opaque pointers are also a good fit for general-purpose libraries like cJSON.

Is there any disadvantage to using an Opaque Pointer?

Yes, using an opaque pointer prevents us from allocating the structures from the stack. Thus, if we have one function call where we need a structure for the duration of the call but don't need it after the function has returned, we are still forced to use malloc() for the structure, and malloc() is much slower than stack allocation.

Conclusion

In this article, we have discussed opaque pointers. We see where we should use opaque pointers and why we need them. We also understand this with the help of an example in which we see a problem statement, and then we see how we can give the solution to that problem statement.

To learn more, see Basics of C++ with Data StructureDBMSOperating System by Coding Ninjas, and keep practicing on our platform Coding Ninjas Studio.If you think you are ready for the tech giants company, check out the mock test series on code studio. You can also refer to our Guided Path on Coding Ninjas Studio to upskill yourself in domains like Data Structures and AlgorithmsCompetitive ProgrammingAptitude, and many more!. You can also prepare for tech giants companies like Amazon, Microsoft, Uber, etc., by looking for the questions asked by them in recent interviews. If you want to prepare for placements, refer to the interview bundle. If you are nervous about your interviews, you can see interview experiences to get the ideas about questions that have been asked by these companies.

 Do upvote if you find this blog helpful!

Be a Ninja

Happy Coding!

Live masterclass