Re: Using array slices with C-style fread() from file

2017-06-21 Thread Ali Çehreli via Digitalmars-d-learn
On 06/21/2017 12:20 PM, uncorroded wrote: > So @trusted is me guaranteeing to the compiler that this > function is safe? Yes and you shouldn't lie. :) > Is there any way of auditing this code through > valgrind, etc. That's a good idea, which you can do yourself but I don't think you can be

Re: Using array slices with C-style fread() from file

2017-06-21 Thread uncorroded via Digitalmars-d-learn
On Wednesday, 21 June 2017 at 19:11:44 UTC, Ali Çehreli wrote: On 06/21/2017 12:06 PM, uncorroded wrote: > Is > there any way of making the function with @safe as well? I get the > errors "cannot call @system function 'core.stdc.stdio.fread,fopen,fclose'. @trusted is exactly for that. It can

Re: Using array slices with C-style fread() from file

2017-06-21 Thread Ali Çehreli via Digitalmars-d-learn
On 06/21/2017 12:06 PM, uncorroded wrote: > Is > there any way of making the function with @safe as well? I get the > errors "cannot call @system function 'core.stdc.stdio.fread,fopen,fclose'. @trusted is exactly for that. It can be called from @safe code: @trusted @nogc ubyte[n]

Re: Using array slices with C-style fread() from file

2017-06-21 Thread uncorroded via Digitalmars-d-learn
On Wednesday, 21 June 2017 at 18:58:58 UTC, tetyys wrote: On Wednesday, 21 June 2017 at 18:49:01 UTC, uncorroded wrote: Is there a way of making this work with D slices? Can they be used as C-style pointers? What about this: @nogc ubyte[n] rand_bytes(uint n)() { import core.stdc.stdio;

Re: Using array slices with C-style fread() from file

2017-06-21 Thread tetyys via Digitalmars-d-learn
On Wednesday, 21 June 2017 at 18:58:58 UTC, tetyys wrote: On Wednesday, 21 June 2017 at 18:49:01 UTC, uncorroded wrote: Is there a way of making this work with D slices? Can they be used as C-style pointers? What about this Or simpler, while (left) left -= fread(buf[n-left .. $].ptr,

Re: Using array slices with C-style fread() from file

2017-06-21 Thread Ali Çehreli via Digitalmars-d-learn
- Fixed-length arrays are value types. You may not want to return them if n is very large. - Every array has the .ptr property that gives the pointer to the first element. - What can be helpful to you here is the fact that you can treat any memory as a slice: import core.stdc.stdlib;

Re: Using array slices with C-style fread() from file

2017-06-21 Thread tetyys via Digitalmars-d-learn
On Wednesday, 21 June 2017 at 18:49:01 UTC, uncorroded wrote: Is there a way of making this work with D slices? Can they be used as C-style pointers? What about this: @nogc ubyte[n] rand_bytes(uint n)() { import core.stdc.stdio; FILE *fp; fp = fopen("/dev/urandom", "r");

Using array slices with C-style fread() from file

2017-06-21 Thread uncorroded via Digitalmars-d-learn
Hi all, I am writing a program to read device /dev/urandom file to get some random bytes. I am trying to bypass D's GC as I know the length of the buffer needed. I tried using std.stdio.File and rawRead but File cannot be used with @nogc. So I used core.stdc.stdio and used traditional C