3

I'm currently using opaque pointers as my standard technique for encapsulation, but looking at the OpenGL API makes me think that using id numbers could be a better choice. I would like some advice from seasoned C programmers (I've only been using the language actively for ~2 years).

Here are my initial thoughts that I would like confirmed or corrected.

Possible pros for Id numbers:

  • Using object/memory pools in the implementation is rather straight forward if using id numbers

  • The id number does not have to map to the system memory (could reference graphics memory in the GL case)

Possible cons for Id numbers:

  • Makes the implementation slightly more complex

There is a similar question that takes into account the situation of using shared libraries: Should I use integer ID or pointers for my opaque objects? My question is not about shared libraries, it's about the general case of hiding implementation details from user code.

I suppose you could typedef a MyObjectHandle to enable the library to switch between id number and opaque pointer.

The question is: What are the pros and cons for opaque pointers vs id numbers using the C programming language?

Community
  • 1
  • 1
arkod
  • 1,973
  • 1
  • 20
  • 20
  • 1
    "put on hold as primarily opinion-based" yes, I would like to hear some opinion on this subject. haha.... wtf? – arkod Oct 06 '13 at 20:13
  • changing title to Jeopardy! style. (formulated as a question) – arkod Oct 07 '13 at 11:31
  • Yes, SO is broken by design. Usually I find the most valuable answers in questions that were closed because of their broken rules. The problem is when a question gets closed before receiving answers, like in this case. I came this looking for answers on the topic you wrote. – cesss Aug 07 '20 at 09:45

0 Answers0