Type safety could prevent nuclear war

anonymous via Digitalmars-d digitalmars-d at puremagic.com
Thu Feb 4 15:40:13 PST 2016


On 04.02.2016 23:57, tsbockman wrote:
> http://www.underhanded-c.org/#winner
>
> Actually, I'm surprised that this works even in C - I would have
> expected at least a compiler (or linker?) warning; this seems like it
> should be easy to detect automatically.

You can do the same thing in D, using extern(C) to get no mangling:

main.d:
----
alias float_t = double;
extern(C) float_t deref(float_t* a);
void main()
{
     import std.stdio: writeln;
     float_t d = 1.23;
     writeln(deref(&d)); /* prints "1.01856e-314" */
}
----

deref.d:
----
alias float_t = float;
extern(C) float_t deref(float_t* a) {return *a;}
----

Command to build and run:
----
dmd main.d deref.d && ./main
----



More information about the Digitalmars-d mailing list