473,406 Members | 2,549 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,406 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 17285
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
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.