www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 8722] New: foreach triggers a floating point exception with multidimensional array of a dimension equal to 0

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

           Summary: foreach triggers a floating point exception with
                    multidimensional array of a dimension equal to 0
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: blooh_ hotmail.com


--- Comment #0 from Christopher Crouzet <blooh_ hotmail.com> 2012-09-25
01:56:23 PDT ---
The code below triggers a floating point exception, not sure if it's too be
expected?

// ------------------------------
import std.stdio;
import std.traits;


void whatever( T )( in ref T array )
{
  static if ( isArray!( T ) )
    foreach ( element; array )
      whatever( element );
}


void main()
{
  int[0][2][3] array;
  whatever( array );
}
// ------------------------------



If changing the loop to 

// ------------------------------
    foreach ( i; 0 .. array.length )
      whatever( array[i] );
// ------------------------------

it works fine.


Also changing the array type to int[0][2] ie. works as well.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 25 2012
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8722



--- Comment #1 from Christopher Crouzet <blooh_ hotmail.com> 2012-09-25
02:10:09 PDT ---
Did I really write "too be"...? Where's the edit button!? :)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 25 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8722


monarchdodra gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |monarchdodra gmail.com


--- Comment #2 from monarchdodra gmail.com 2012-09-25 02:26:59 PDT ---
Additional information:

The first example makes my dmd 2.060 crash actually (!)

Note that the two examples are not *strictly* equivalent, as
    foreach(element)
will make a copy of each element, whereas:
    foreach ( i; 0 .. array.length )
      whatever( array[i] )
Will access each element by reference.

Changing the first code snippet to:

//-----------------------------
void whatever( T )( in ref T array )
{
  static if ( isArray!( T ) )
    foreach ( ref element; array ) //HERE: REF
      whatever( element );
}


void main()
{
  int[0][2][3] array;
  whatever( array );
}
//-----------------------------

Works.

Either way, there is something wrong in there, if dmd is crashing. Besides, the
code is legit, and should not produce a floating point exception either.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 25 2012