www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - index overflow

reply "Bent Rasmussen" <exo bent-rasmussen.info> writes:
struct vector (T, uint N)
{

    T[N] S;

    .vector!(T,N*2) opCat(vector a);

}

alias vector!(float,3) float3;

void main()
{
}

dmd.exe test.d
test.d(5): index -1073741824 overflow for static array XP DMD 0.97
Jul 31 2004
parent reply h3r3tic <h3r3tic dev.null> writes:
Bent Rasmussen wrote:

 struct vector (T, uint N)
 {
 
     T[N] S;
 
     .vector!(T,N*2) opCat(vector a);
 
 }
 
 alias vector!(float,3) float3;
 
 void main()
 {
 }
 
 
dmd.exe test.d
test.d(5): index -1073741824 overflow for static array XP DMD 0.97
I dont't think it's a bug except for the signed output. What happens here is that vector is instantiated recursively by having N = 3, then 3*2, 3*4, 3*8, 3*16, 3*32, ... until it reaches 3*2^30. It's equal 3221225472 then. 3*2^30 when stored in a signed int == -1073741824. If that 3*2^30 were to be multiplied by 2 once more, it would overflow the dword storage it's given and be equal 6*2^30 == 3*2^31 and thats more than 2^32-1 which cannot be handled by a dword. Here, the compiler properly signals that something is wrong. It could give some more meaningful information though, at least by printing the number as an unsigned int. I'm not sure but I think it's a downside of the en-masse template instantiation we have in D. havent tried this kind of thing in C++ though... regards, Tom
Jul 31 2004
parent "Bent Rasmussen" <exo bent-rasmussen.info> writes:
You're right, I was too quick on that one.
Jul 31 2004