www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - D int and C/C++ int etc not really compatible when interfacing to

reply Shriramana Sharma via Digitalmars-d-learn writes:
In the following pages:

http://dlang.org/interfaceToC.html
http://dlang.org/cpp_interface

the "Data Type Compatibility" section says D int is compatible with
C/C++ int. Isn't this actually false because D's integer types are
fixed-size whereas C/C++'s are variable? So D int is only compatible
with C++ int32_t, and who knows what that is typedef-ed to? Likewise
for uint, long, short etc.

So how to ensure, when calling C/C++, that the D int etc are being
mapped to the correctly-sized C/C++ type? Does the compiler ensure
that since the compatibility is being advertised as built-in?

-- 
Shriramana Sharma ஶ்ரீரமணஶர்மா
श्रीरमणशर्मा
Nov 01 2014
next sibling parent reply "Kagamin" <spam here.lot> writes:
D claims compatibility with system C compiler, which usually have 
32-bit int.
Nov 01 2014
parent reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
On Saturday, 1 November 2014 at 21:00:54 UTC, Kagamin wrote:
 D claims compatibility with system C compiler, which usually 
 have 32-bit int.
... and for C/C++ long which can be 32 or 64 bit, DMD recently introduced the types c_long and c_ulong. (Not released yet.)
Nov 02 2014
next sibling parent reply Mike Parker <aldacron gmail.com> writes:
On 11/2/2014 8:59 PM, "Marc Schütz" <schuetzm gmx.net>" wrote:
 On Saturday, 1 November 2014 at 21:00:54 UTC, Kagamin wrote:
 D claims compatibility with system C compiler, which usually have
 32-bit int.
... and for C/C++ long which can be 32 or 64 bit, DMD recently introduced the types c_long and c_ulong. (Not released yet.)
Those aren't new. They have been in core.stdc.config for quite some time.
Nov 02 2014
next sibling parent "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
On Sunday, 2 November 2014 at 12:37:13 UTC, Mike Parker wrote:
 On 11/2/2014 8:59 PM, "Marc Schütz" <schuetzm gmx.net>" wrote:
 On Saturday, 1 November 2014 at 21:00:54 UTC, Kagamin wrote:
 D claims compatibility with system C compiler, which usually 
 have
 32-bit int.
... and for C/C++ long which can be 32 or 64 bit, DMD recently introduced the types c_long and c_ulong. (Not released yet.)
Those aren't new. They have been in core.stdc.config for quite some time.
Ah, I see. But DMD now has support for mangling these types correctly, in commit c4e0de64.
Nov 02 2014
prev sibling parent reply "ponce" <contact gam3sfrommars.fr> writes:
On Sunday, 2 November 2014 at 12:37:13 UTC, Mike Parker wrote:
 On 11/2/2014 8:59 PM, "Marc Schütz" <schuetzm gmx.net>" wrote:
 On Saturday, 1 November 2014 at 21:00:54 UTC, Kagamin wrote:
 D claims compatibility with system C compiler, which usually 
 have
 32-bit int.
... and for C/C++ long which can be 32 or 64 bit, DMD recently introduced the types c_long and c_ulong. (Not released yet.)
Those aren't new. They have been in core.stdc.config for quite some time.
c_long and c_ulong get used, should c_int and c_uint too in bindings? Looks like fringe use case.
Nov 02 2014
parent "Sean Kelly" <sean invisibleduck.org> writes:
On Sunday, 2 November 2014 at 16:53:06 UTC, ponce wrote:
 c_long and c_ulong get used, should c_int and c_uint too in 
 bindings?
 Looks like fringe use case.
On common 32 and 64-bit platforms, the only type whose size changed between 32 and 64 bits is long, so the other aliases were deemed unnecessary. It's possible that as D is ported to more platforms this will have to change, but I seriously hope not.
Nov 02 2014
prev sibling parent "Sean Kelly" <sean invisibleduck.org> writes:
On Sunday, 2 November 2014 at 11:59:27 UTC, Marc Schütz wrote:
 On Saturday, 1 November 2014 at 21:00:54 UTC, Kagamin wrote:
 D claims compatibility with system C compiler, which usually 
 have 32-bit int.
... and for C/C++ long which can be 32 or 64 bit, DMD recently introduced the types c_long and c_ulong. (Not released yet.)
c_long and c_ulong have existed as aliases in core.stdc since the beginning. The change was to make them an explicit type so name mangling could be different.
Nov 02 2014
prev sibling parent "John Colvin" <john.loughran.colvin gmail.com> writes:
On Saturday, 1 November 2014 at 15:00:57 UTC, Shriramana Sharma 
via Digitalmars-d-learn wrote:
 In the following pages:

 http://dlang.org/interfaceToC.html
 http://dlang.org/cpp_interface

 the "Data Type Compatibility" section says D int is compatible 
 with
 C/C++ int. Isn't this actually false because D's integer types 
 are
 fixed-size whereas C/C++'s are variable? So D int is only 
 compatible
 with C++ int32_t, and who knows what that is typedef-ed to? 
 Likewise
 for uint, long, short etc.

 So how to ensure, when calling C/C++, that the D int etc are 
 being
 mapped to the correctly-sized C/C++ type? Does the compiler 
 ensure
 that since the compatibility is being advertised as built-in?
Note this at the end of the "Data Type Compatiblity" section: "These equivalents hold for most C compilers. The C standard does not pin down the sizes of the types, so some care is needed." D's built-in types do not change based on the companion C compiler. When linking to C, it is the programmers responsibility to ensure they are using the right types. core.stdc.config can help deal with the most common inconsistencies.
Nov 02 2014