Reading a file eats whole memory

Frank Benoit keinfarbton at googlemail.com
Sun Oct 21 08:45:54 PDT 2007


Emil Wojak schrieb:
> Hi!
> 
> Could someone please explain why this code tries to eat my 1 GB memory
> and gets killed by the kernel afterwards? Eventually it prints "Error:
> Out of memory" when I set ulimit on memory prior to launching the program.
> 
> The code:
> import std.stream;
> 
> int main(char [][] args) {
>     Stream input=new File(args[0]);
> 
>     char[] data;
>     input.read(data);
>     input.close();
>     return 0;
> }
> 
> My intention was to read the executable itself, which is about 444 kB.
> I'm running Linux, compiling with Digital Mars D Compiler v1.022

other had commented the file reading...

Using arg[0] to access the programs binary is not save, because if it is
called via the PATH variable it does not contain the path.

/proc/self/exe is a link to your executable.




More information about the Digitalmars-d-learn mailing list