Problem while creating 2D array at run time.
Hi all,
I have a macros and a method like below for creating 2D array from local variable,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#define CREATE_2D_ARRAY(dest, source, dim1, dim2)\
{\
dest = new int*[dim1] ; \
for(int _i = 0; _i < dim1; _i++) \
{\
dest[_i] = create1DArray( source[_i], dim2 ) ; \
}\
}
template <class Ttype>Ttype* create1DArray(Ttype* source, const int length)
{
if (length == 0)
{
return NULL ;
}
Ttype* dest = new Ttype[length] ;
for(int _i = 0; _i < length; _i++)
{
dest[_i] = source[_i];
}
return dest;
}
|
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
int _temp[2][2] =
{
{
2,
3,
},
{
4,
5,
},
};
int** rids;
CREATE_2D_ARRAY(rids , _temp , 2,2);
|
But for the same example if i use following macros it is crashing can any one please tell me the reason???
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#define CREATE_2D_ARRAY(dest, source, dim1, dim2)\
{\
dest = new int*[dim1] ; \
for(int _i = 0; _i < dim1; _i++) \
{\
CREATE_1D_ARRAY(dest[_i],source[_i], dim2 ) ; \
}\
}
#define CREATE_1D_ARRAY(dest, source, dim1)\
{\
dest = new int[dim1] ; \
for(int _i = 0; _i < dim1; _i++) \
{\
dest[_i] = source[_i];\
}\
}
|
Both macros use _i as a variable name, so when you "invoke" CREATE_1D_ARRAY from CREATE_2D_ARRAY, 2D's _i is shadowed by 1D's.
Topic archived. No new replies allowed.