 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Michael J. Reeves, AA, AS Guest
|
Posted: Tue Sep 07, 2004 11:18 pm Post subject: Multi-Dimensional Dynamic Arrays |
|
|
Sorry to bother, again, but I am not understanding the process
to create a dynamic multi-dimensional array.
From my previous postings and responses, I have:
int* array = new int [Size_Of_Array];
Now, I need to be able to expand this to at least 2-dimensions.
| Quote: | From my reading, it appears that this is a 2-step process? More steps
for more dimensions? |
From programming in other languages, it seems to me that there
must be a shortcut to this somewhere?
Something like:
int* array = new int [Row][Column];
Where Row & Column are not fixed integers, but dynamically
calculated, also.
Insights and suggestions welcome?
THANKS
Michael J. Reeves, AA, ASc
E-Mail: [email]michaeljreeves (AT) sbcglobal (DOT) net[/email]
---------------------------------------------------------
I have no SPAM. I don't give a SPAM.
I take no SPAM from anyone. I am NOT in the SPAM business!!!
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maxim Yegorushkin Guest
|
Posted: Wed Sep 08, 2004 10:38 am Post subject: Re: Multi-Dimensional Dynamic Arrays |
|
|
Michael J. Reeves, AA, ASc <michaeljreeves (AT) sbcglobal (DOT) net> wrote:
| Quote: | Sorry to bother, again, but I am not understanding the process
to create a dynamic multi-dimensional array.
From my previous postings and responses, I have:
int* array = new int [Size_Of_Array];
Now, I need to be able to expand this to at least 2-dimensions.
From my reading, it appears that this is a 2-step process? More steps
for more dimensions?
|
You may not expand it, rather you may represent is.
As arrays in C++ are contiguous, you can in fact represent a
one-dimensional array as n-dimensional. All you have to know is the
dimensions. Here is an example:
#include <cassert>
#include <cstddef>
template<size_t x_dim, size_t y_dim, class T>
inline T(&as_dim(T* a))[x_dim][y_dim]
{
return reinterpret_cast<T(&)[x_dim][y_dim]>(*a);
}
template<size_t x_dim, size_t y_dim, size_t z_dim, class T>
inline T(&as_dim(T* a))[x_dim][y_dim][z_dim]
{
return reinterpret_cast<T(&)[x_dim][y_dim][z_dim]>(*a);
}
int main()
{
int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
int eight_1 = a[8];
int eight_2 = as_dim<2, 5>(a)[1][3];
int eight_3 = as_dim<3, 2, 2>(a)[2][0][0];
assert(8 == eight_1);
assert(8 == eight_2);
assert(8 == eight_3);
}
| Quote: | From programming in other languages, it seems to me that there
must be a shortcut to this somewhere?
Something like:
int* array = new int [Row][Column];
Where Row & Column are not fixed integers, but dynamically
calculated, also.
|
You can create here a one-dimensional array:
int* array = new int[Row * Column];
and access its elements at [x, y] like:
int elem = array[x * Column + y];
--
Maxim Yegorushkin
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
gorda Guest
|
Posted: Wed Sep 08, 2004 9:52 pm Post subject: Re: Multi-Dimensional Dynamic Arrays |
|
|
"Michael J. Reeves, AA, ASc" <michaeljreeves (AT) sbcglobal (DOT) net> wrote
| Quote: | Sorry to bother, again, but I am not understanding the process
to create a dynamic multi-dimensional array.
From my previous postings and responses, I have:
int* array = new int [Size_Of_Array];
Now, I need to be able to expand this to at least 2-dimensions.
From my reading, it appears that this is a 2-step process? More steps
for more dimensions?
From programming in other languages, it seems to me that there
must be a shortcut to this somewhere?
Something like:
int* array = new int [Row][Column];
Where Row & Column are not fixed integers, but dynamically
calculated, also.
Insights and suggestions welcome?
THANKS
Michael J. Reeves, AA, ASc
E-Mail: [email]michaeljreeves (AT) sbcglobal (DOT) net[/email]
---------------------------------------------------------
I have no SPAM. I don't give a SPAM.
I take no SPAM from anyone. I am NOT in the SPAM business!!!
|
Create a one dimensional array of pointers (with the length equivilant
to the number of rows you need) and make EACH pointer a dynamically
allocated one dimentional array (with the length equivilant to the
number of columns you need).
//Dynamically create a 3 by 4 array:
int rows=3, cols=4, int **x;
//create one dimensional array of pointers
x = new int*[rows];
//initialize each pointer to be another one dimentional array
for(int i = 0; i < rows; ++i)
{
x[i] = new int[cols];
}
//Can use x just like a normal array now.
x[2][2] = 4;
cout << x[2][2];
Also, you might want to see question 6.16 in the C programming FAQ,
though of course, that uses C type malloc semantics. The ideas however
are valid in both C and C++. I dont know if there is a similar
question in the C++ FAQ.
http://www.eskimo.com/~scs/C-faq/q6.16.html
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Daniel Krügler (ne Spange Guest
|
Posted: Wed Sep 08, 2004 9:58 pm Post subject: Re: Multi-Dimensional Dynamic Arrays |
|
|
Hello Michael Reeves,
Michael J. Reeves, AA, ASc schrieb:
| Quote: | Sorry to bother, again, but I am not understanding the process
to create a dynamic multi-dimensional array.
From my previous postings and responses, I have:
int* array = new int [Size_Of_Array];
Now, I need to be able to expand this to at least 2-dimensions.
From my reading, it appears that this is a 2-step process? More steps
for more dimensions?
From programming in other languages, it seems to me that there
must be a shortcut to this somewhere?
Something like:
int* array = new int [Row][Column];
Where Row & Column are not fixed integers, but dynamically
calculated, also.
Insights and suggestions welcome?
THANKS
|
You could use the following way:
int** array = new int*[Row];
for (size_t i = 0; i < Row; ++i){
array[i] = new int[Col];
}
but I don't recommend it, especially if you want to transfer such arrays
to Fortran programs. Better use a matrix wrapper (e.g. have a look into
boost), which basically uses a row-column-index mapping and allocates a single
block
int* array = new int[Row*Col];
Now this matrix wrapper class usually provides user-defined operators to
access the elements, either {const}T& operator()(size_t i, size_t j) or
two depending operator[] overloads to hide the index mapping from you,
either the Fortran columns major way, i.e.
array[i * Col + j]
or the C row major way:
array[j * Row + i]
Greetings from Bremen,
Daniel Krügler
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Michael J. Reeves, AA, AS Guest
|
Posted: Fri Sep 10, 2004 2:13 pm Post subject: Re: Multi-Dimensional Dynamic Arrays |
|
|
On 8 Sep 2004 17:52:02 -0400, [email]smith4894 (AT) excite (DOT) com[/email] (gorda) wrote:
| Quote: | "Michael J. Reeves, AA, ASc" <michaeljreeves (AT) sbcglobal (DOT) net> wrote
Sorry to bother, again, but I am not understanding the process
to create a dynamic multi-dimensional array.
From my previous postings and responses, I have:
int* array = new int [Size_Of_Array];
Now, I need to be able to expand this to at least 2-dimensions.
From my reading, it appears that this is a 2-step process? More steps
for more dimensions?
From programming in other languages, it seems to me that there
must be a shortcut to this somewhere?
Something like:
int* array = new int [Row][Column];
Where Row & Column are not fixed integers, but dynamically
calculated, also.
Insights and suggestions welcome?
THANKS
Michael J. Reeves, AA, ASc
E-Mail: [email]michaeljreeves (AT) sbcglobal (DOT) net[/email]
---------------------------------------------------------
I have no SPAM. I don't give a SPAM.
I take no SPAM from anyone. I am NOT in the SPAM business!!!
Create a one dimensional array of pointers (with the length equivilant
to the number of rows you need) and make EACH pointer a dynamically
allocated one dimentional array (with the length equivilant to the
number of columns you need).
//Dynamically create a 3 by 4 array:
int rows=3, cols=4, int **x;
//create one dimensional array of pointers
x = new int*[rows];
//initialize each pointer to be another one dimentional array
for(int i = 0; i < rows; ++i)
{
x[i] = new int[cols];
}
//Can use x just like a normal array now.
x[2][2] = 4;
cout << x[2][2];
Also, you might want to see question 6.16 in the C programming FAQ,
though of course, that uses C type malloc semantics. The ideas however
are valid in both C and C++. I dont know if there is a similar
question in the C++ FAQ.
http://www.eskimo.com/~scs/C-faq/q6.16.html
|
THANKS. I think this was the approach I was trying to implement.
One of my texts uses the malloc-style of C, and it didn't really make a
lot of sense. Plus the fact that I wasn't able to properly implement.
MJR
Michael J. Reeves, AA, ASc
E-Mail: [email]michaeljreeves (AT) sbcglobal (DOT) net[/email]
---------------------------------------------------------
I have no SPAM. I don't give a SPAM.
I take no SPAM from anyone. I am NOT in the SPAM business!!!
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Michael J. Reeves, AA, AS Guest
|
Posted: Wed Sep 22, 2004 10:07 am Post subject: Re: Multi-Dimensional Dynamic Arrays |
|
|
On 8 Sep 2004 17:52:02 -0400, [email]smith4894 (AT) excite (DOT) com[/email] (gorda) wrote:
| Quote: | Create a one dimensional array of pointers (with the length equivilant
to the number of rows you need) and make EACH pointer a dynamically
allocated one dimentional array (with the length equivilant to the
number of columns you need).
//Dynamically create a 3 by 4 array:
int rows=3, cols=4, int **x;
//create one dimensional array of pointers
x = new int*[rows];
//initialize each pointer to be another one dimentional array
for(int i = 0; i < rows; ++i)
{
x[i] = new int[cols];
}
//Can use x just like a normal array now.
x[2][2] = 4;
cout << x[2][2];
The above will not compile and run. |
Suggestions.
MJR
Michael J. Reeves, AA, ASc
E-Mail: [email]michaeljreeves (AT) sbcglobal (DOT) net[/email]
---------------------------------------------------------
I have no SPAM. I don't give a SPAM.
I take no SPAM from anyone. I am NOT in the SPAM business!!!
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Fri Sep 24, 2004 11:31 am Post subject: Re: Multi-Dimensional Dynamic Arrays |
|
|
"Michael J. Reeves, AA, ASc" <michaeljreeves (AT) sbcglobal (DOT) net> wrote in
message news:<2pc1l0dr2vho6qvuuk7pi2k5p80lbibff8 (AT) 4ax (DOT) com>...
| Quote: | On 8 Sep 2004 17:52:02 -0400, [email]smith4894 (AT) excite (DOT) com[/email] (gorda) wrote:
Create a one dimensional array of pointers (with the length
equivilant to the number of rows you need) and make EACH pointer a
dynamically allocated one dimentional array (with the length
equivilant to the number of columns you need).
//Dynamically create a 3 by 4 array:
int rows=3, cols=4, int **x;
//create one dimensional array of pointers
x = new int*[rows];
//initialize each pointer to be another one dimentional array
for(int i = 0; i < rows; ++i)
{
x[i] = new int[cols];
}
//Can use x just like a normal array now.
x[2][2] = 4;
cout << x[2][2];
The above will not compile and run.
|
Where's the problem? It works with my compilers.
--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Eric Tetz Guest
|
Posted: Sat Sep 25, 2004 5:40 am Post subject: Re: Multi-Dimensional Dynamic Arrays |
|
|
Michael J. Reeves AA, ASc wrote:
| Quote: | The above will not compile and run.
|
There's a pretty obvious syntax error in the first line. The code
is otherwise fine.
I wouldn't create a 2D array that way. Requires rows+1 allocations.
It's much better to allocate one contiguous block for the data and
a second array for indexing into the data. Requires 2 allocations:
T* block = new T[rows * cols];
T** array = new T*[rows];
for (size_t i = 0; i < rows; ++i)
array[i] = block + cols * i;
// use 'array' as normal 2D array
delete[] *array;
delete[] array;
Cheers,
Eric
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Michael J. Reeves, AA, AS Guest
|
Posted: Sat Sep 25, 2004 10:15 am Post subject: Re: Multi-Dimensional Dynamic Arrays |
|
|
On 24 Sep 2004 07:31:48 -0400, [email]kanze (AT) gabi-soft (DOT) fr[/email] wrote:
| Quote: | "Michael J. Reeves, AA, ASc" <michaeljreeves (AT) sbcglobal (DOT) net> wrote in
message news:<2pc1l0dr2vho6qvuuk7pi2k5p80lbibff8 (AT) 4ax (DOT) com>...
On 8 Sep 2004 17:52:02 -0400, [email]smith4894 (AT) excite (DOT) com[/email] (gorda) wrote:
Create a one dimensional array of pointers (with the length
equivilant to the number of rows you need) and make EACH pointer a
dynamically allocated one dimentional array (with the length
equivilant to the number of columns you need).
//Dynamically create a 3 by 4 array:
int rows=3, cols=4, int **x;
//create one dimensional array of pointers
x = new int*[rows];
//initialize each pointer to be another one dimentional array
for(int i = 0; i < rows; ++i)
{
x[i] = new int[cols];
}
//Can use x just like a normal array now.
x[2][2] = 4;
cout << x[2][2];
The above will not compile and run.
Where's the problem? It works with my compilers.
|
THAT's what I needed to know.
I am using Borland C++ Builder 5, and I find that some examples
in the texts I am using do NOT compile.
The one above submitted by a reader mimics an example from my
text, and neither will compile under this system.
I should probably start a new thread to deal with this question
of compatability among compilers.
Again, THANKS.
MJR
Michael J. Reeves, AA, ASc
E-Mail: [email]michaeljreeves (AT) sbcglobal (DOT) net[/email]
---------------------------------------------------------
I have no SPAM. I don't give a SPAM.
I take no SPAM from anyone. I am NOT in the SPAM business!!!
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Cesar Rabak Guest
|
Posted: Sun Sep 26, 2004 9:34 am Post subject: Re: Multi-Dimensional Dynamic Arrays -- nit pick |
|
|
Eric Tetz escreveu:
| Quote: | Michael J. Reeves AA, ASc wrote:
Suggestions?
I wouldn't create a 2D array that way. Requires rows+1 allocations.
It's much better to allocate one contiguous block for the data and
a second array for indexing into the data. Requires 2 allocations:
T* block = new T[rows * cols];
T** array = new T*[rows];
for (size_t i = 0; i < rows; ++i)
array[i] = block + cols * i;
// use 'array' as normal 2D array
delete[] *array;
delete[] array;
|
Isn't the 'delete[] array' supposed to be 'delete[] block'?
--
Cesar Rabak
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Zian Smith Guest
|
Posted: Mon Sep 27, 2004 8:43 pm Post subject: Re: Multi-Dimensional Dynamic Arrays -- nit pick |
|
|
Cesar Rabak <crabak (AT) acm (DOT) org> wrote
| Quote: | Eric Tetz escreveu:
Michael J. Reeves AA, ASc wrote:
Suggestions?
I wouldn't create a 2D array that way. Requires rows+1 allocations.
It's much better to allocate one contiguous block for the data and
a second array for indexing into the data. Requires 2 allocations:
T* block = new T[rows * cols];
T** array = new T*[rows];
for (size_t i = 0; i < rows; ++i)
array[i] = block + cols * i;
// use 'array' as normal 2D array
delete[] *array;
delete[] array;
Isn't the 'delete[] array' supposed to be 'delete[] block'?
|
No, you can't replace 'delete[] array' with 'delete[] block'. However,
you can replace 'delete[] *array' with 'delete block' if you wish.
To clarify,
*array = *(array + 0) = array[0] = block, which is the starting
address of the block array.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Eric Tetz Guest
|
Posted: Mon Sep 27, 2004 8:48 pm Post subject: Re: Multi-Dimensional Dynamic Arrays -- nit pick |
|
|
Cesar Rabak wrote:
| Quote: | delete[] *array;
delete[] array;
Isn't the 'delete[] array' supposed to be 'delete[] block'?
|
That's taken care of by 'delete[] *array' -- which means
you don't have to pass around two pointers to keep track
of the array.
Cheers,
Eric
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Michael Karcher Guest
|
Posted: Wed Oct 06, 2004 11:00 pm Post subject: Re: Multi-Dimensional Dynamic Arrays -- nit pick |
|
|
Zian Smith <smith7005 (AT) yahoo (DOT) com> wrote:
| Quote: | T* block = new T[rows * cols];
T** array = new T*[rows];
for (size_t i = 0; i < rows; ++i)
array[i] = block + cols * i;
// use 'array' as normal 2D array
delete[] *array;
delete[] array;
No, you can't replace 'delete[] array' with 'delete[] block'. However,
you can replace 'delete[] *array' with 'delete block' if you wish.
|
You mean 'delete[] block' instead of 'delete block'.
Michael Karcher
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Zian Smith Guest
|
Posted: Fri Oct 08, 2004 1:16 am Post subject: Re: Multi-Dimensional Dynamic Arrays -- nit pick |
|
|
[email]Michael.Karcher (AT) writeme (DOT) com[/email] (Michael Karcher) wrote in message news:<2siiqeF1lqbqdU1 (AT) uni-berlin (DOT) de>...
| Quote: | Zian Smith <smith7005 (AT) yahoo (DOT) com> wrote:
T* block = new T[rows * cols];
T** array = new T*[rows];
for (size_t i = 0; i < rows; ++i)
array[i] = block + cols * i;
// use 'array' as normal 2D array
delete[] *array;
delete[] array;
No, you can't replace 'delete[] array' with 'delete[] block'. However,
you can replace 'delete[] *array' with 'delete block' if you wish.
You mean 'delete[] block' instead of 'delete block'.
Michael Karcher
|
Yes, you're right, my mistake..
Can replace 'delete[] *array' with 'delete[] block' to free the entire array.
-Z.Smith
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|