The interface to C is designed according to the design, and D should be good with C cooperation with the target system. D Depending on the C operation time library in the target environment, it compensates for some defects that lack of standard virtual machines. It is not significant to write the existing C API with D re-use. Touch them directly to be much easier? !
This can be done if the same data type, memory distribution, and function call / return instruction sequence is used.
Call C function D can call C functions directly. No packaging functions, parameter transformations, and C functions do not need to be placed in separate DLLs.
The C function must be declared as a "C" call convention, for example:
Extern (c) int strcmp (char * string1, char * string2);
Then you can call them in D code:
IMPORT std.string;
INT mydfunction (char [] s)
{
Return strcmp (std.string.tocharz (s), "foo / 0");
}
Need to pay attention to the following points:
D know the conversion rules of the C function name and the call / return instruction sequence of the function. The C function cannot be overloaded by another C function of the same name. There is no C-type modifier such as __cdecl, __ far, __ stdcall, __far, __ stdcall, __ decspec, or so like this. These functions are implemented by "feature", such as Extern (C). There is no Const or Volatile type modifier in D. If you want to declare a C function that uses these type modifiers, you only need to remove these keywords from the declaration. The string in d is not ending with 0. See "Data Type Compatibility". Accordingly, as long as the D function is compatible with the C compiler, the most likely is extern (c), and the C code can also call D functions:
// myfunc () can be called by any C function
Extern (c)
{
Void MyFunc (int A, int b)
{
...
}
}
Storage assignment C code manages memory by calling Malloc () and Free (). D Distribute memory using the D garbage collection program, so you don't need to explicitly release memory.
D can still be explicitly assigned and released using C.stdlib.malloc () and C.stdlib.Free (), which can be used to link those such as the Malkoc buffer C function.
If the pointer to the memory allocated by the collection program is passed to the C function, you must ensure that this memory is not recycled by the memory collection program before using its C function. To achieve this goal, you can:
Create a copy of the copy using C.STDLIB.malloc (), passes the copy to the C function. Place the pointer on the stack (as a parameter or auto variable) because the garbage collection program will scan the stack. The pointer to which it will point to the static data segment, because the garbage collection program scans the static data segment. Use gc.addroot () or gc.addrange () to manage the pointer to the garbage collection program. Even if the pointer inside the allocated memory block is also enough to let the GC know the object is being used; that is, do not necessarily need to maintain the pointer to the assigned memory.
The garbage collection program does not scan the stack of threads created by the D Thread interface, nor does it scan data segments in other DLLs.
Data type compatibility
Type C Type D Type voidvoidbit no equivalent bytesigned charubyteunsigned charcharchar (D is the unsigned chars) wcharwchar_tshortshortushortunsigned shortintintuintunsignedlonglong longulongunsigned long longfloatfloatdoubledoubleextendedlong doubleimaginarylong double _Imaginarycomplexlong double _Complextype * type * type [dim] type [dim] type [] No equivalent type TYPE [TYPE] No equivalent type "String / 0" "String" or L "string" Class No equivalent type Type (*) Type (*) (parameters) For most 32-bit C compiler Say, the above correspondence is established. The C standard does not constrain the size of the type, so be careful when using this correspondence. The main problem called Printf () is how the PrintF format indicator matches the corresponding D data type. Despite the design, Printf can only handle a string ending with 0, and cannot process D's CHAR dynamic arrays, but it is proved that since the structure of the D dynamic array is {length, pointing to data pointer}, Printf
%. * s format works very well:
Void foo (char [] string)
{
Printf ("My String IS:%. * S / N", String);
}
Anemissile reader will notice that the printf format string is not ending with '/ 0'. This is because if the character string is not the initial value of the data structure, a secondary '/ 0' is stored at the end.
The structure of the structure and the combination D is similar in the joint C.
C code typically uses command line options or various #pragma instructions provided by various implementations to specify alignment or tightening methods. D supports explicit alignment features corresponding to the C compiler rules. You can view how C code is aligned, and then the D structure is aligned.
D does not support the bit field. You can use shift and Mask operations to simulate if needed.
Interface D of C does not provide an interface to C . However, because D provides an interface to C, it is accessible to the C function using the C link.
D of the class object is not compatible with C class objects.