473,398 Members | 2,380 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,398 software developers and data experts.

Multidimensional arrays

What's the best way of dynamically allocating a multidim array from the
following:

int **a = new int*[size];
a[0] = new int[size];
.... and so on

or:

int (*a)[10] = new int[10][10];
by the way, how do we pass arrays delcared by the second way to
functions? What really is the difference between (*a)[] and *a[]..
actually i kind of understand the difference but it doesn't hit me that
well.. I can't interpret what (*a)[] really means. Thanks!

James

Jul 19 '05 #1
4 22442

"Buster" <no***@nowhere.com> wrote in message
"James" <sn****@jamison.snort> wrote in message
What's the best way of dynamically allocating a multidim array from the
following:

int **a = new int*[size];
a[0] = new int[size];
... and so on
Use this way if the last dimension is not a compile-time constant.


I should have said 'any dimension except the first'.

You can also have 'rows' (say) with different numbers of 'columns'
using this idiom.
or:

int (*a)[10] = new int[10][10];


This way can be simpler but only works if the last dimension is a
compile-time constant.


And here, 'every dimension except the first'.
Regards,
Buster.
Jul 19 '05 #2

"James" <sn****@jamison.snort> wrote in message
news:3f********@clarion.carno.net.au...
What's the best way of dynamically allocating a multidim array from the
following:

int **a = new int*[size];
a[0] = new int[size];
... and so on

or:

int (*a)[10] = new int[10][10];
by the way, how do we pass arrays delcared by the second way to
functions? What really is the difference between (*a)[] and *a[]..
actually i kind of understand the difference but it doesn't hit me that
well.. I can't interpret what (*a)[] really means. Thanks!

James


I use a pointer to an array of pointers to an array of <type>, like this

int **array;

array = new int*[width];
for (int loop=0; loop<width; ++loop)
array[loop] = new int[height];

// do some things with it like
// array[3][4] = rand(); // or whatever

for (loop=0; loop<width; ++loop)
delete []array[loop];
delete [] array;

Allan
Jul 19 '05 #3

James <sn****@jamison.snort> wrote in message
news:3f********@clarion.carno.net.au...
What's the best way of dynamically allocating a multidim array from the
following:

int **a = new int*[size];
a[0] = new int[size];
... and so on

or:

int (*a)[10] = new int[10][10];


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

-Mike

Jul 19 '05 #4

Here's a class that creates a 2 dimensional array very easily and
efficiently:

template < class T, int ROW_T = 0, int COL_T = 0 >

class dynamic_2d_array

{

public:

dynamic_2d_array(int row, int col):m_row(row),m_col(col),
m_data((row!=0&&col!=0)?new T[row*col]:NULL){}

dynamic_2d_array():m_row(ROW_T),m_col(COL_T), m_data(new
T[ROW_T*COL_T])

{if (!COL_T || !ROW_T) {int x[ROW_T] = {{ROW_T}};int y[COL_T] =
{{x[0]}};}}

~dynamic_2d_array(){if(m_data) delete []m_data;}

T* operator[](int i) {return (m_data + (m_col*i));}

T const*const operator[](int i) const {return (m_data + (m_col*i));}

private:

const int m_row;

const int m_col;

T* m_data;

};

The above class can be delcare and used in the following maner:

dynamic_2d_array < char > My_dynamic_2d_array(3, 20);

My_dynamic_2d_array[2][11] = 99;

cout << My_dynamic_2d_array[2][11] << endl;

You can also use a vector<vector<char> > type object.

For more information on this class, see the following link:

http://axter.com/faq/topic.asp?TOPIC...CAT_ID=9&Topi-
c_Title=How+to+create+dynamic+two%2Ddimensional+ar rays&Forum_Title=-
C%2FC%2B%2B

Also check out the multidimensional class in the following link:

http://axter.com/faq/topic.asp?TOPIC...AT_ID=9&Topic-
_Title=How+to+create+dynamic+Multi%2Ddimensional+a rrays&Forum_Title=-
C%2FC%2B%2B
--
Top ten Expert at Experts-Exchange
Posted via http://dbforums.com
Jul 19 '05 #5

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

Similar topics

5
by: Golf Nut | last post by:
I am finding that altering and affecting values in elements in multidimensional arrays is a huge pain in the ass. I cannot seem to find a consistent way to assign values to arrays. Foreach would...
2
by: Terry | last post by:
Hi, can someone plz tell me how multidimensional arrays (like a 2-D array) are stored in memory? Are they like single dimensional arrays? Stored sequentially in one "row", so to say? Thanks ...
9
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
3
by: Claire | last post by:
I have a multidimensional array defined as private double myArray = new double; The first column of the array contains X values, the other contains Y values I have a charting function defined as...
3
by: Ravi Singh (UCSD) | last post by:
Hello all I am trying to use jagged and multi-dimensional arrays in C++. In C# these work fine // for jagged arrays string jaggedArray = new string ; //for multidimensional arrays string...
21
by: utab | last post by:
Hi there, Is there a way to convert a double value to a string. I know that there is fcvt() but I think this function is not a part of the standard library. I want sth from the standard if...
10
by: | last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily complicated than the other languages I know. I feel this is because there aren't many good official resources out there to...
2
by: oopsatwork | last post by:
Ok...so, I have been outside of the C world for a _very_ long time...but not so long as to remember how to do multidimensional arrays. So, let me state that I know how to malloc pointers to...
12
by: filippo nanni | last post by:
Hello everybody, my question is this: I have two multidimensional arrays and I have to create a third one (for later use) from comparing these two. Here is my example code: //BEGIN CODE var...
9
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.