[Issue 1875] New: &null[x] compiles and runs, giving x

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Feb 27 14:10:51 PST 2008


http://d.puremagic.com/issues/show_bug.cgi?id=1875

           Summary: &null[x] compiles and runs, giving x
           Product: D
           Version: 1.027
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Keywords: accepts-invalid, spec
          Severity: trivial
          Priority: P5
         Component: DMD
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: matti.niemenmaa+dbugzilla at iki.fi


import std.stdio;
void main() {
        writefln(&null[42]);
}

The above prints "002A" - i.e. the hexadecimal representation of 42, the value
used to index null.

This is, I'm fairly certain, unspecified behaviour, but I think you'd be hard
pressed to argue that this makes sense in any way. I'm marking this as
"accepts-invalid" but also as "trivial" since I honestly hope that nobody else
has ever dreamed of doing something like this. :-)

The issue is essentially: why does null[x] return a void type, and why is
taking its address a valid operation? In addition, why is indexing null valid
in the first place? Trying the same with [][x] results in out-of-bounds errors
at compile time.

Note that this works even at runtime, for any value of x.


-- 



More information about the Digitalmars-d-bugs mailing list