-2

I have read this code snippet somewhere but I am not able to understand its meaning.

/** Use strong typing for ODP types */
#define odp_handle_t struct {} *  

What is significance of above code snippet?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Jagdish
  • 1,848
  • 3
  • 22
  • 33
  • 1
    [An excellent answer to a related question](http://stackoverflow.com/a/430414/434551) that might answer your question. – R Sahu Jun 02 '15 at 05:40

2 Answers2

2

This code snippet defines the symbol odp_handle_t which represents an opaque handle to a resource. It's opaque because it's a pointer to an empty struct. The thing that it's pointing to is not exposed to the user. It's called a handle because it does not point directly to the resource, it just identifies it. The internal implementation knows how to use this handle to access the required resource. This helps maintain independence between the client code and the implementation of the API. Finally, the strong typing part comes from the fact that it's a pointer to a type (the empty struct) as opposed to a void pointer.

Hadi Brais
  • 22,259
  • 3
  • 54
  • 95
1

That is just some nonsense code not really related to strong typing. It appears to be some non-standard way of declaring a pointer to incomplete type, but it is not valid C.

#define odp_handle_t struct {} *  

odp_handle_t x; // will not compile, struct has no members

I believe this is yet another pointless gcc extension. Also, hiding away pointers behind typedefs is always a very bad idea.

There is no reason why you can't declare your pointer to incomplete/opaque type with pure standard C, and you can do so without hiding pointers behind typedefs:

h file

typedef struct odp_handle_t odp_handle_t;

c file

struct odp_handle_t
{
  // have to put struct members in here
};

caller c file

odp_handle_t *pointer_to_incomplete_type;
Lundin
  • 195,001
  • 40
  • 254
  • 396
  • What are the struct members that you would put in there? – Hadi Brais Jun 02 '15 at 06:46
  • @HadiBrais Whatever is needed for the program to keep as private variables, not exposed nor available to the caller. – Lundin Jun 02 '15 at 06:51
  • @Ludin Like what exactly? All types of handles that I've seen are defined as either void pointers or empty struct pointers. – Hadi Brais Jun 02 '15 at 06:56
  • @HadiBrais The "handle" is the pointer to incomplete type. What goes into the struct definition is entirely up to the specific application. Take for example the `FILE` handle in standard C, it is most likely implemented in this very manner. To the user it is just some kind of abstract pointer, but to the compiler implementation it will point to a lot of internal information regarding the opened file. – Lundin Jun 02 '15 at 08:52
  • FILE is not a handle. From section 7.21.1 and 7.21.3 of the C standard, it's defined to be an object type representing a stream to access a file. By looking at its definition in stdio.h, we can see that it's a complete type. The file handle is a field of that type called _file of type int, which is also sometimes used as a type of handles. – Hadi Brais Jun 02 '15 at 09:35
  • @HadiBrais Alright so bad example. Anyway this isn't rocket science, it is how you design ADTs with private encapsulation in C... – Lundin Jun 02 '15 at 13:16