extern and opaque structs

Jonathan M Davis jmdavisProg at gmx.com
Wed May 2 22:22:22 PDT 2012


On Thursday, May 03, 2012 07:00:21 James Miller wrote:
> I'm doing C bindings and I have an opaque struct and an extern'd
> variable of the type of that struct. The problem is that dmd is
> complaining that the struct has no definition (which is true).
> Making it a pointer works (expected) but i can't do that because
> the interface is expecting an full struct.
> 
> Adding __gshared doesn't help.
> 
> I assume this is bug, since usage of extern means that I don't
> need to know the size, since it will be allocated in the C code,
> not the D code.

If you're putting the struct on the stack, then you need the struct's 
definition. That's true in C/C++ as well as in D. You can get away without 
having the definitions to its _functions_, but you need the definition for the 
struct itself. The only time that you escape this is via pointers.

And I don't know how you can talk about the struct as being allocated in the C 
code if it's on the stack, except perhaps if it has pointers as member 
variables, in which case it's the memory for those member variables which is 
allocated in the C code. The entire struct is copied every time that it's 
passed to anything - unless you're using pointers, which you say you aren't.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list