Blitz logo

Blitz Support :

From: Julian Cummings (cummings_at_[hidden])
Date: 2005-05-20 20:25:24


Hello Faheem,

The depth value that you are getting for the 2-dimensional Array is
obviously bogus. You are accessing an element in the TinyVector length_
that is out of bounds. Bounds checking (and other sanity checks) are
enabled by compiling with -DBZ_DEBUG. If you compile your example with
this flag and run it, it will report the illegal element access and
halt. Without such checking, the call to depth() will just happily hand
you the next piece of data within the Array object, which happens to be
an 8 (the first element of the TinyVector stride_). It's a good idea to
always build a blitz code in debug mode including the -DBZ_DEBUG flag
and make sure everything runs OK, then rebuild in optimized mode without
this flag to remove the run-time checks.

In the case where A is a 3-dimensional Array, blitz allows you to
provide fewer length parameters than the number of dimensions. It will
automatically use the final length argument for all remaining
dimensions. This is just a convenience for lazy code writers. I
personally don't like this feature because it can lead to unintended
consequences. So my advice is to take care to always provide the proper
number of length arguments or use the convenient blitz shape() function
to produce a TinyVector of Array extents. For example, the line

Array<int,3> A(shape(8,8));

fails to compile because the Array constructor will not accept a
TinyVector of extents with the wrong rank. So it forces you to write

Array<int,3> A(shape(8,8,1));

which produces the expected results in your example code.

Hope this helps,
Julian C.

On Fri, 2005-05-20 at 16:55 -0400, Faheem Mitha wrote:
> Hi,
>
> This code seems quite happy to return a value for a non-existent 3rd
> dimension extent. Even if A is declared as
>
> Array<int, 3> A(8, 8);
>
> it still returns 8. Shouldn't it throw an exception in both cases?
>
> Also, I notice that the compiler does not force the number of constructor
> arguments to be equal to the dimension of the array. I presume this is by
> design, but what is the reason?
>
> Thanks in advance.
> Faheem.
>
> #include <blitz/array.h>
> using namespace blitz;
>
> int main()
> {
> Array<int,2> A(8,8);
> A = 0;
>
> cout << "Rows = " << A.rows() << endl;
> cout << "Cols = " << A.cols() << endl;
> // bug in blitz? depth() is equivalent to extent(thirdDim)
> // but A.depth returns 8 instead of 1
> cout << "Depth = " << A.depth() << endl;
> cout << "Dim = " << A.rank() << endl;
>
> return 0;
> }
>
> _______________________________________________
> Blitz-support mailing list
> Blitz-support_at_[hidden]
> http://www.oonumerics.org/mailman/listinfo.cgi/blitz-support
>

-- 
Dr. Julian C. Cummings                       E-mail: cummings_at_[hidden]
California Institute of Technology           Phone:  626-395-2543
1200 E. California Blvd., Mail Code 158-79   Fax:    626-584-5917
Pasadena, CA 91125                           Office: 125 Powell-Booth