Carl
Tue Jun 06 09:02:52 CDT 2006
Tosha wrote:
> "Alex Blekhman" <xfkt@oohay.moc> wrote in message
> news:OqnhGuViGHA.4144@TK2MSFTNGP02.phx.gbl...
>> Jack wrote:
>>> In java, you can extract the height and width of an
>>> array. In C++, how can u do the same?
>>> In java, I do this
>>> int width = img[0].length;
>>> int height = img.length;
>>>
>>> Type of img is int[][]
>>
>> In C++ you can do this only if dimensions of an array were known at
>> compile time:
>>
>> int a[2][3];
>>
>> int width = sizeof(a[0])/sizeof(a[0][0]);
>> int height = sizeof(a)/sizeof(a[0]);
>>
>> If you allocate array dynamically, there is no way to know its actual
>> dimentions. That's why you should use std::vector as Mark Randall
>> suggests. std::vector has member function size().
>
>
http://msdn2.microsoft.com/en-us/library/z2s077bc(VS.80).aspx
>
> #include <cstdlib>
> #include <cstdio>
> #include <malloc.h> // Req. by _msize
>
> ---
> void PrintMatNoSizeGiven(int **mat)
> {
> for (int i = 0; i<_msize(mat)/4 ; i++)
... which isn't portable, and only works if the memory was allocated by
malloc (or new[], which calls malloc in the VC++ implementation).
in general, this won't work, and it can't tell you the individual dimensions
of a multidimensional "rectangular array" (which you've shown here is the C
equivalent of a "jagged array" that just happens to be rectangular.
Still, this technique will work in some situations.
-cd