Manually allocating a File

Chris M. chrismohrfeld at comcast.net
Tue Feb 20 14:56:54 UTC 2018


I'm doing this mainly for experimentation, but the following 
piece of code gives all sorts of errors. Hangs, segfaults or 
prints nothing and exits

import std.stdio;
import core.stdc.stdlib;

void main()
{
     auto f = cast(File *) malloc(File.sizeof);
     *f = File("test.txt", "r");
     (*f).readln.writeln;
     // freeing is for chumps
}

I could have sworn I've done something similar recently and it 
worked, unfortunately I can't remember what the case was.

This is what gdb gave me on a segfault

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a56c32 in 
_D2rt5minfo__T17runModuleFuncsRevSQBgQBg11ModuleGroup8runDtorsMFZ9__lambda1ZQCkMFAxPyS6object10ModuleInfoZv ()
    from /usr/lib64/libphobos2.so.0.78

So it looks like it's reaching the end of main. Past that I can't 
tell what's going on. Ideas?


More information about the Digitalmars-d-learn mailing list