Bug 2078 – Bizzarre array index out of bounds error with union

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
PowerPC
OS
Mac OS X
Creation time
2008-05-07T22:32:00Z
Last change time
2015-06-09T05:14:50Z
Assigned to
dvdfrdmn
Creator
aronnax

Attachments

IDFilenameSummaryContent-TypeSize
254vector.dSource listingtext/plain1497

Comments

Comment #0 by aronnax — 2008-05-07T22:32:02Z
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.
Comment #1 by aronnax — 2008-05-07T22:33:04Z
Created attachment 254 Source listing
Comment #2 by fawzi — 2008-05-09T07:34:05Z
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
Comment #3 by aronnax — 2008-05-09T11:40:23Z
Whoops! I don't know what I was thinking.