On 12/20/2011 07:12 PM, bearophile wrote:
Stewart Gordon:

On 19/12/2011 12:12, bearophile wrote:

Try something like this (untested):

alias float TF;
TF[] f = (cast(TF*)std.gc.malloc(x * TF.sizeof))[0 .. x];
<snip>

I fail to see any real difference from the OP's code:

- Why the alias?

Because in that code I have used three times a type (TF), auto allows to remove 
only one of them. The alias is not the best solution (a better solution is to 
put that code into a templated function), but repeating the same generic type 
more than one time is usually a source of bugs.


- std.gc.malloc returns the array with correct length according to my quick 
test, so the
use of [0..x] is redundant

Really? Well, as I have said I have not tested that code.
Generally GC functions return a void*, so to create an array I think you need 
to slice it... What is the code of your quick test?


- using TF.sizeof instead of 4 might fix things if on the user's platform float 
isn't 4
bytes long.

In D I think float is always 4 bytes long.


  Otherwise, while using .sizeof instead of a hard-coded number is better
practice, it isn't going to get rid of an AV.

I don't know what an AV is.

Bye,
bearophile

Access Violation.

Reply via email to