473,221 Members | 1,922 Online

# find length of a dynamic array

hey everyone

i'm trying to make a function that will return the length and width of
a dynamically allocated 2D array.

here's what i came up with to find width:

int findWidth(int** matrix)
{
int width = 0;

while(matrix[width][0] != NULL)
width++;

return width;
}

this doesnt work though. part of the problem is that my matrix has many
zeros. and i think this function treats those zeros as NULL, and doesnt
return the right value.

who has other ideas?

Jul 6 '06 #1
6 17264
already made small progress on my own:

int findWidth(int** matrix)
{
int width = 0;

keyword &
width++;

return width;
}
this seems to work to find the width of the 2d array.

but when i try to find length by editing the one line to:
while(&matrix[0][width] != NULL) //switched [0] and [width]

-- i get an infinite loop :(

i appreciate any help that is offered

Jul 6 '06 #2
fi***********@gmail.com wrote:
already made small progress on my own:

int findWidth(int** matrix)
{
int width = 0;

keyword &
width++;

return width;
}
this seems to work to find the width of the 2d array.

but when i try to find length by editing the one line to:
while(&matrix[0][width] != NULL) //switched [0] and [width]

-- i get an infinite loop :(

i appreciate any help that is offered
This method won't work. Compare:

http://www.parashift.com/c++-faq-lit...html#faq-16.14

You either need to keep track of the height and width yourself or,
better, use a container class that automates it. See, e.g., the Matrix
class defined here:

http://www.parashift.com/c++-faq-lit...html#faq-16.19

Cheers! --M

Jul 6 '06 #3
posted:
hey everyone

i'm trying to make a function that will return the length and width of
a dynamically allocated 2D array.

Maybe something like:
#define LengthOf(arr) (sizeof(arr) / sizeof(*arr))
#define WidthOf(arr) (sizeof(*arr) / sizeof(**arr))

#include <iostream>

int main()
{
int array1[5][3];
int array2[3][1];
int array3[4][4];
int array4[1][7];
int array5[8][3];
int array6[5][6];
int array7[3][7];
int array8[7][3];
int array9[2][7];

std::cout << LengthOf(array1) << ' ' << WidthOf(array1) << '\n';
std::cout << LengthOf(array2) << ' ' << WidthOf(array2) << '\n';
std::cout << LengthOf(array3) << ' ' << WidthOf(array3) << '\n';
std::cout << LengthOf(array4) << ' ' << WidthOf(array4) << '\n';
std::cout << LengthOf(array5) << ' ' << WidthOf(array5) << '\n';
std::cout << LengthOf(array6) << ' ' << WidthOf(array6) << '\n';
std::cout << LengthOf(array7) << ' ' << WidthOf(array7) << '\n';
std::cout << LengthOf(array8) << ' ' << WidthOf(array8) << '\n';
std::cout << LengthOf(array9) << ' ' << WidthOf(array9) << '\n';

}
Or perhaps if you would prefer templates:
#include <cstddef>

struct Dimensions {

std::size_t length;
std::size_t width;

};
template<class T,std::size_t i,std::size_t j>
Dimensions GetD( T const (&arr)[i][j] )
{
Dimensions tmp = { i, j };

return tmp;

/* return (Dimensions){i,j}; */
}

#include <iostream>

int main()
{
int array1[5][3];
int array2[3][1];
int array3[4][4];
int array4[1][7];
int array5[8][3];
int array6[5][6];
int array7[3][7];
int array8[7][3];
int array9[2][7];

std::cout << GetD(array1).length << ' ' << GetD(array1).width <<
'\n';
std::cout << GetD(array2).length << ' ' << GetD(array2).width <<
'\n';
std::cout << GetD(array3).length << ' ' << GetD(array3).width <<
'\n';
std::cout << GetD(array4).length << ' ' << GetD(array4).width <<
'\n';
std::cout << GetD(array5).length << ' ' << GetD(array5).width <<
'\n';
std::cout << GetD(array6).length << ' ' << GetD(array6).width <<
'\n';
std::cout << GetD(array7).length << ' ' << GetD(array7).width <<
'\n';
std::cout << GetD(array8).length << ' ' << GetD(array8).width <<
'\n';
std::cout << GetD(array9).length << ' ' << GetD(array9).width <<
'\n';

}
--

Frederick Gotham
Jul 6 '06 #4

"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:YH*******************@news.indigo.ie...
posted:
>hey everyone

i'm trying to make a function that will return the length and width of
a dynamically allocated 2D array.

Maybe something like:
#define LengthOf(arr) (sizeof(arr) / sizeof(*arr))
#define WidthOf(arr) (sizeof(*arr) / sizeof(**arr))
These won't work for "a dynamically allocated 2D array".
Jul 6 '06 #5
Michael Taylor posted:

> #define LengthOf(arr) (sizeof(arr) / sizeof(*arr))
#define WidthOf(arr) (sizeof(*arr) / sizeof(**arr))

These won't work for "a dynamically allocated 2D array".

If you want to create an array, the first thing to consider is:

(1) Will the dimension(s) be known at compile time?

If so, then you create it in the ordinary fashion:

int array[64];

If not, then you must use dynamic allocation:

int * const p = new int[len];

int * const p = (int*)malloc(len * sizeof(int));
It is possible to dynamically allocate an array and STILL keep track of its
dimensions:

int (&array)[64] = *new int[1][64];

However this can't be used if the dimensions aren't known until runtime:

int (&array)[len] = *new int[1][len]; /* Compile ERROR */

Therefore, there is in fact NO WAY to find out the dimensions of a
dynamically allocated array, unless you yourself keep track of it.
--

Frederick Gotham
Jul 6 '06 #6
fi***********@gmail.com wrote:
already made small progress on my own:

int findWidth(int** matrix)
{
int width = 0;

keyword &
width++;

return width;
}
this seems to work to find the width of the 2d array.

but when i try to find length by editing the one line to:
while(&matrix[0][width] != NULL) //switched [0] and [width]

-- i get an infinite loop :(

i appreciate any help that is offered
I recommend that you not use this type of C-style array, and instead
use std::vector.
You can create a 2D array with vector by using a vector of vector
vector< vector< int matrix;
You can then easily determine it's size by using the vector::size()
method.

If you still need to use raw pointers, then you'll need some type of
implementation define method to determine the size.
For example, VC++ has an _msize function that can be used to determine
the size of dynamically allocated pointer. But that is not part of the
C++ standard, and therefore not portable.

Jul 7 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.