Interfacing with c and platform dependent sizes

Jacob Carlborg doob at me.com
Sat Feb 26 02:49:30 PST 2011


On 2011-02-26 01:28, simendsjo wrote:
> C is not my strong side, so I'm having some problems wrapping some code.
>
> I found a couple of sources on this:
> 1) http://www.digitalmars.com/d/2.0/htomodule.html
> 2) http://www.digitalmars.com/d/2.0/interfaceToC.html
>
> 1)
> C's long is the same as D's int.
> long long is long
>
> 2)
> C 32bit's long long is D's long, C 64 bits long is D's long.
>
> So.. A long in C is the same as the platform size? And long long doesn't
> exist in 64 bit?

In general you can follow the table at 
http://www.digitalmars.com/d/2.0/htomodule.html

But when it comes to "long" in C you have to be careful. On 32bit 
platforms a C "long" will be 32bit long. But on 64bit platforms it 
depends of what data model is used. To simplify things:

* On Windows 64bit a C "long" will be 32bit long
* On Posix 64bit a C "long" will be 64bit long.

What you can do is to create an alias called "c_long" and "c_ulong", 
something like this:

version (D_LP64)
{
     version (Windows)
     {
         alias int c_long;
         alias uint c_ulong;
     }

     else
     {
         alias long c_long;
         alias ulong c_ulong;
     }
}

else
{
     alias int c_long;
     alias uint c_ulong;
}

To read more about data models: 
http://en.wikipedia.org/wiki/64-bit#Specific_C-language_data_models

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list