There’s many instances of code similar to this in the codebase I’m working in these days. This is a thin C wrapper aroundthe OS API.
// From the OS
HANDLE CreateObject();
void CloseHandle(HANDLE);
typedef struct tagFOO {} FOO;
FOO* Foo_New()
{
return (FOO*)CreateObject();
}
void Foo_Delete(FOO* foo)
{
if(foo != NULL)
{
CloseHandle((HANDLE)foo);
}
}
void Foo_Bar(FOO* foo)
{
if(foo != NULL)
{
HANDLE h = (HANDLE)foo;
// Do something interesting with h
}
}
This seems to work and I want to avoid touching it if I can, but is this well defined? It seems very fishy to me
The C99 standard says:
Officially, that precludes your empty structure definition (and so does §6.5.2.1 in the C89 standard); there should be at least one member in the structure. However, you have to push GCC fairly hard to get it to complain; I used
-std=c99 -pedanticand only with the-pedanticdid it warn ‘struct has no members’.In my view, you would be better off (more strictly portable) with:
Or, even using: