www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Array length : size_t

reply "Namespace" <rswhite4 googlemail.com> writes:
D's Array length is currently of type size_t, which means on 32 
bit it's an uint and on 64 bit an ulong. This is difficult: What 
if I want to give the length of an array as parameter to some C 
functions which accepts only an int?
What is the right/safe way to do this? A cast? Or is there 
something better?
I like to avoid cast's if they aren't necessary.
Sep 18 2013
next sibling parent "Dicebot" <public dicebot.lv> writes:
On Wednesday, 18 September 2013 at 20:46:21 UTC, Namespace wrote:
 D's Array length is currently of type size_t, which means on 32 
 bit it's an uint and on 64 bit an ulong. This is difficult: 
 What if I want to give the length of an array as parameter to 
 some C functions which accepts only an int?
 What is the right/safe way to do this? A cast? Or is there 
 something better?
 I like to avoid cast's if they aren't necessary.
Perfectly safe way is to do run-time range check followed by a cast. Maybe using a wrapper: auto shrinkTo(Target, Source)(Source source) if (is(Target : Source)) // is implicitly convertable other way around { assert(source <= Target.max); static if (is(typeof(Taget.min))) assert(source >= Target.min); return cast(Target)source; } void main() { import std.stdio; long a = 50; long b = int.max; b++; writeln(shrinkTo!int(a)); writeln(shrinkTo!int(b)); }
Sep 18 2013
prev sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Wed, Sep 18, 2013 at 10:46:18PM +0200, Namespace wrote:
 D's Array length is currently of type size_t, which means on 32 bit
 it's an uint and on 64 bit an ulong. This is difficult: What if I
 want to give the length of an array as parameter to some C functions
 which accepts only an int?
 What is the right/safe way to do this? A cast? Or is there something
 better?
 I like to avoid cast's if they aren't necessary.
If the C function only accepts int, then just use to!int(array.size). If the size overflows int, to() will throw an exception which you can handle. This is probably the best you can do anyway, since if the C function doesn't take anything bigger than int, then there's no way you can pass the real size to it. T -- PNP = Plug 'N' Pray
Sep 18 2013
next sibling parent reply "Dicebot" <public dicebot.lv> writes:
On Wednesday, 18 September 2013 at 22:20:45 UTC, H. S. Teoh wrote:
 If the C function only accepts int, then just use 
 to!int(array.size). If
 the size overflows int, to() will throw an exception which you 
 can
 handle. This is probably the best you can do anyway, since if 
 the C
 function doesn't take anything bigger than int, then there's no 
 way you
 can pass the real size to it.
Ah, `to!()` does check valid ranges before conversion? Good to know, thanks.
Sep 18 2013
parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Thu, Sep 19, 2013 at 12:52:55AM +0200, Dicebot wrote:
 On Wednesday, 18 September 2013 at 22:20:45 UTC, H. S. Teoh wrote:
If the C function only accepts int, then just use to!int(array.size).
If the size overflows int, to() will throw an exception which you can
handle. This is probably the best you can do anyway, since if the C
function doesn't take anything bigger than int, then there's no way
you can pass the real size to it.
Ah, `to!()` does check valid ranges before conversion? Good to know, thanks.
Yes, it's handled explicitly by the following overload of toImpl() in std.conv: T toImpl(T, S)(S value) if (!isImplicitlyConvertible!(S, T) && (isNumeric!S || isSomeChar!S || isBoolean!S) && (isNumeric!T || isSomeChar!T || isBoolean!T) && !is(T == enum)) { ... } T -- Too many people have open minds but closed eyes.
Sep 18 2013
prev sibling parent reply "Namespace" <rswhite4 googlemail.com> writes:
On Wednesday, 18 September 2013 at 22:20:45 UTC, H. S. Teoh wrote:
 On Wed, Sep 18, 2013 at 10:46:18PM +0200, Namespace wrote:
 D's Array length is currently of type size_t, which means on 
 32 bit
 it's an uint and on 64 bit an ulong. This is difficult: What 
 if I
 want to give the length of an array as parameter to some C 
 functions
 which accepts only an int?
 What is the right/safe way to do this? A cast? Or is there 
 something
 better?
 I like to avoid cast's if they aren't necessary.
If the C function only accepts int, then just use to!int(array.size). If the size overflows int, to() will throw an exception which you can handle. This is probably the best you can do anyway, since if the C function doesn't take anything bigger than int, then there's no way you can pass the real size to it. T
So to!int is safer but slower and a cast would be unsafe but faster?
Sep 19 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Namespace:

 So to!int is safer but slower and a cast would be unsafe but 
 faster?
Right. Bye, bearophile
Sep 19 2013
parent "Namespace" <rswhite4 googlemail.com> writes:
On Thursday, 19 September 2013 at 11:10:08 UTC, bearophile wrote:
 Namespace:

 So to!int is safer but slower and a cast would be unsafe but 
 faster?
Right. Bye, bearophile
Thanks!
Sep 19 2013