www.digitalmars.com         C & C++   DMDScript  

D.gnu - [Issue 2078] New: Bizzarre array index out of bounds error with union

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2078

           Summary: Bizzarre array index out of bounds error with union
           Product: DGCC aka GDC
           Version: unspecified
          Platform: Macintosh
        OS/Version: Mac OS X
            Status: NEW
          Severity: normal
          Priority: P2
         Component: glue layer
        AssignedTo: dvdfrdmn users.sf.net
        ReportedBy: aronnax umd.edu


I am writing a generic vector geometry package that looks like this:

union vector(T, int N)
{
// ...
// A whole bunch of overloaded operators
// ...

T[N] data;
static if (N == 2)
{
struct { T x, y; };
}
else static if (N == 3)
{
struct { T x, y, z; };
}
else static if (N == 4)
{
struct { T w, x, y, z; };
}

string toString()
{
string s;
for (int i = 0 ; i < N - 1 ; i ++)
s += format("%.23f, ", data[i]);
s += format("%.23f", data[N]); // Line 73
return s;
}
}

typedef vector!(float, 3) vector3f;
typedef vector!(float, 4) vector4f;
typedef vector!(double, 3) vector3d;
typedef vector!(double, 4) vector4d;

And I am getting the following compile error:
vector.d:73: Error: array index 3 is out of bounds this.data[0 .. 3]
vector.d:73: Error: array index 4 is out of bounds this.data[0 .. 4]
vector.d:73: Error: array index 3 is out of bounds this.data[0 .. 3]
vector.d:73: Error: array index 4 is out of bounds this.data[0 .. 4]

See the attached source file.
Compiler is dgcc(subversion)+gcc-4.1.2 on Mac OS X 10.5.2.


-- 
May 07 2008
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2078






Created an attachment (id=254)
 --> (http://d.puremagic.com/issues/attachment.cgi?id=254&action=view)
Source listing


-- 
May 07 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2078


fawzi gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID





repeat with me:

"D (like C) array indexing is zero based:
int [N] a has N elements from 0 to N-1 (not from 1 to N)"

;)

Fawzi


-- 
May 09 2008
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2078






Whoops!  I don't know what I was thinking.


-- 
May 09 2008