Open MPI with D

Andrej Mitrovic andrej.mitrovich at gmail.com
Fri Nov 25 01:31:30 PST 2011


Taken with modifications from std.process.execv:

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

const(char)** argsToC(in string[] argv)
{
    auto _argv = cast(const(char)**)malloc((char*).sizeof * (1 + argv.length));
    toAStringz(argv, _argv);
    return _argv;
}


private void toAStringz(in string[] a, const(char)** az)
{
    foreach(string s; a)
    {
        *az++ = toStringz(s);
    }

    *az = null;
}

void main()
{
    string[] args = ["foo", "bla"];
    auto _argv = argsToC(args);
    assert(to!string(_argv[0]) == "foo");
    assert(to!string(_argv[1]) == "bla");
}

Take a look at the various execv/execvp in std.process, but be careful
since most of them use alloca to allocate on the stack.


More information about the Digitalmars-d mailing list