473,221 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 software developers and data experts.

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;

while(&matrix[width][0] != NULL) //added memory address
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;

while(&matrix[width][0] != NULL) //added memory address
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;

while(&matrix[width][0] != NULL) //added memory address
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.

Similar topics

1
by: Daniel | last post by:
By using this code I made an array from a hashed array to regain a table content and layout as received from a mysql database... my $sth = $dbh->prepare("SELECT * FROM `Groepen';");...
7
by: murrayatuptowngallery | last post by:
Hello: I previously posted a question about how to do populate an html table dynamically with results from JavaScript Math and basic math. Dr Clue responded and this started the learning curve....
2
by: stealth_spoof | last post by:
Hi People wondering if anyone can help me with a problem I'm having I'm trying to create an array with an unspecified length, the length is based on the result i get from another task in the code...
1
by: vmoreau | last post by:
I have a text and I need to find a Word that are not enclosed in paranthesis. Can it be done with a regex? Is someone could help me? I am not familar with regex... Example looking for WORD:...
43
by: Frodo Baggins | last post by:
Hi all, We are using strcpy to copy strings in our app. This gave us problems when the destination buffer is not large enough. As a workaround, we wanted to replace calls to strcpy with strncpy....
10
by: Zytan | last post by:
This article: http://www.c-sharpcorner.com/UploadFile/mahesh/WorkingWithArrays11142005060354AM/WorkingWithArrays.aspx claims that "In C#, arrays can be declared as fixed length or dynamic". I...
2
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
4
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
11
by: C C++ C++ | last post by:
Hi all, got this interview question please respond. How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Rgrds MA
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.