CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Guest

    Dynamic Multidimensional Arrays

    Hallo Gurus

    I've done the following. It works.


    float *array = new float[m_numbRows];




    What I like to do is the following:

    float *array = new float[m_numbRows][m_numbRows];




    But it doesn't work.


    I've got the following help from a friend. We think there should be an easier way.

    float** pvfArray;

    .....later....

    pvfArray = new float*[iNumRows];
    for(int i = 0; i < iNumRows; i++)
    pvfArray[i] = new float[iNumCols];

    pvfArray[iR][iC] = 3.9;




    Thanks for the help.

    M



  2. #2
    Guest

    Re: Dynamic Multidimensional Arrays

    Have you tried:

    float *array = new float[m_numbCol*m_numbRows];

    array[7][9] = 25; //or
    array[(7*m_numbRows) + 9] = 25;

    ...or something similar?




  3. #3
    Join Date
    Apr 1999
    Posts
    19

    Re: Dynamic Multidimensional Arrays

    Thank you very much for your help.

    I got everything to work, except for


    array[7][9] = 25;



    Thanks again for your help.

    Regards
    Meintjes


  4. #4
    Join Date
    Apr 1999
    Posts
    4

    Re: Dynamic Multidimensional Arrays

    When you are to allocate a multi-dimensional array with the new operator, you can do it
    only if all dimensions except for the leftmost array dimension are constant expression.

    Here are some examples:

    #define NUM_ROWS 10
    #define NUM_COLS 10

    float (*array2)[NUM_ROWS];
    float (*array3)[NUM_ROWS][NUM_COLS];

    int first_rows;

    // after somehow the value of the first_rows is determined.

    array2 = new float [first_rows][NUM_ROWS];
    array3 = new float [first_rows][NUM_ROWS][NUM_COLS];

    // When the array buffers are no longer needed, you can use delete operator like the followings.
    delete [] array2;
    delete [] array3;

    If you are to allocate a dynamic multidimensional array without any fixed dimension specifier,
    the advice of your friend is a standard way of allocating a dynamic multidimensional array.

    Regards,
    Yangghi.


  5. #5
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: Dynamic Multidimensional Arrays

    array[7][9] = 25; won't work

    array is a single dimensional array SIMULATING a multi dimensional array

    index = 7 * numRows + 9;
    array[index] = 25;


  6. #6
    Guest

    Re: Dynamic Multidimensional Arrays

    There's a forumla you can use on a raw block of memroy to treat it as a multi-dimensional array.

    You can also take a raw block of memory and initialize it as a multi-dimensional array. Uses more memory but allows you to keep the array[][] schemantics.

    And of course, the easiest route is to use STL.

    I think someone already posted the formula for option one, so here's option two and three...


    static const int ROWS = 10;
    static const int COLS = 10;

    int STORAGE = ( (ROWS * COLS) * (sizeof(float)/sizeof(DWORD)) ) + ROWS;

    float** ppFloat = (float**)new DWORD[STORAGE];

    ::memset(ppFloat,0,STORAGE * sizeof(DWORD));

    for(int index = 0;index < ROWS;++index)
    {
    ppFloat[index] = (float*)((DWORD*)ppFloat + ROWS) +
    index * ((sizeof(float)/sizeof(DWORD)) * ROWS);
    }


    float val = 1.5;


    for(int r = 0;r < ROWS;++r)
    for(int c = 0;c < COLS;++c)
    ppFloat[r][c] = val++;

    for(r = 0;r < ROWS;++r)
    for(int c = 0;c < COLS;++c)
    std::cout << '[' << r << "][" << c << "]: "
    << ppFloat[r][c] << std::endl;



    For STL it helps to create a custom function object to help initializing the array.


    typedef std::vector<float> tFloatCol;
    typedef std::vector<tFloatCol> t2dFloatVec;


    class setdim
    {
    public:
    setdim(const int dim = 1):
    m_dim(dim)
    {}

    void operator()(tFloatCol& col)
    {
    col.resize(m_dim,0);
    }
    int m_dim;
    };




    Then using it is...


    static const int ROWS = 10;
    static const int COLS = 10;

    t2dFloatVec vec;

    vec.assign(ROWS);
    std::for_each(vec.begin(),vec.end(),setdim(COLS));

    float val = 1.5;

    for(int r = 0;r < ROWS;++r)
    for(int c = 0;c < COLS;++c)
    vec[r][c] = val++;

    for(r = 0;r < ROWS;++r)
    for(int c = 0;c < COLS;++c)
    std::cout << '[' << r << "][" << c << "]: " << vec[r][c] << std::endl;




    Hope it helps...

    Mike



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured