Re: need help in dynamic memory alloction of multi-dimensional array
- From: "Alex Blekhman" <tkfx.N05P4M@xxxxxxxxx>
- Date: Tue, 9 May 2006 20:18:10 +0300
Allen Maki wrote:
When I used the 2 following lines in a simple code, the
code will compile with no error:
long (*pprime)[max2];
pprime = new long[max][max2];
But if I separate the two lines by putting the first in a
class
and the second line in the constructor of the same class
I will have the under mentioned error messages.
Can you tell me what is wrong?
Stop reinventing the wheel. Use std::vector:
-----------------
typedef std::vector< long > vector1d;
typedef std::vector< vector1d > vector2d;
const size_t rows = 3;
const size_t cols = 4;
vector2d v2d;
v2d.resize(rows);
// initialize 2d array with zeros
//
for(vector2d::iterator it = v2d.begin();
it != v2d.end();
++it)
{
(*it).resize(cols);
}
// now you can access some element
v2d[0][0] = 42;
-----------------
If you want to avoid `for' statement and make code more
laconic, then there are handy STL functors. Here the 2d
array is initialized in one statement:
-----------------
....
// initialize 2d array with zeros
//
std::for_each(v2d.begin(), v2d.end(),
std::bind2nd(
std::mem_fun_ref(&vector1d::resize), cols));
....
-----------------
HTH
Alex
.
- Follow-Ups:
- Re: need help in dynamic memory alloction of multi-dimensional array
- From: Stefan Näwe
- Re: need help in dynamic memory alloction of multi-dimensional array
- References:
- need help in dynamic memory alloction of multi-dimensional array
- From: Allen Maki
- need help in dynamic memory alloction of multi-dimensional array
- Prev by Date: Re: Warning C4800..... why!
- Next by Date: Re: Warning C4800..... why!
- Previous by thread: Re: need help in dynamic memory alloction of multi-dimensional array
- Next by thread: Re: need help in dynamic memory alloction of multi-dimensional array
- Index(es):
Relevant Pages
|