dynamic multidimensional array allocation using one(and only one) 'new' call

Discussion in 'C++' started by ip4ram@yahoo.com, Jun 28, 2004.

  1. Guest

    I have an old set of library,where I used malloc to allocate
    multidimensional(2 and 3) arrays,using only one malloc call(using void
    pointers).I get the size of the array in run time.Now that I am moving
    to C++,I wish to know if it is possible to allocate a
    multi-dimensional(say 2 and 3) with just one (and only one) call to
    'new'operator.The idea behind this is to have contiguous memory
    locations for array data.Thanks in advance,for all your help.

    Ram
    , Jun 28, 2004
    #1
    1. Advertising

  2. wrote in news: in
    comp.lang.c++:

    > I have an old set of library,where I used malloc to allocate
    > multidimensional(2 and 3) arrays,using only one malloc call(using void
    > pointers).I get the size of the array in run time.Now that I am moving
    > to C++,I wish to know if it is possible to allocate a
    > multi-dimensional(say 2 and 3) with just one (and only one) call to
    > 'new'operator.The idea behind this is to have contiguous memory
    > locations for array data.Thanks in advance,for all your help.
    >



    Yes, but always prefer using a standard container to calling
    new [].

    #include <iostream>
    #include <ostream>
    #include <iomanip>
    #include <vector>

    template < typename T >
    struct vector2d
    {
    vector2d(std::size_t n = 0) : m_Order(n), data( n * n ) {}
    void set_Order(std::size_t n) { data.resize(n * (m_Order = n)); }

    T *operator[](std::size_t off)
    {
    return &(data[off * m_Order]);
    }
    T const *operator[](std::size_t off) const
    {
    return &(data[off * m_Order]);
    }

    private:

    std::size_t m_Order;
    std::vector< T > data;
    };


    int main()
    {
    using namespace std;

    vector2d< int > a(10);
    int i, j;

    for (i = 0; i < 10; ++i)
    {
    for (j = 0; j < 10; ++j)
    {
    a[j] = (i + 1) * (j + 1);
    }
    }

    for (i = 0; i < 10; ++i)
    {
    for (j = 0; j < 10; ++j)
    {
    cout << setw(4) << a[j];
    }
    cout << endl;
    }
    }


    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Jun 28, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken
    Replies:
    24
    Views:
    3,842
    Ben Bacarisse
    Nov 30, 2006
  2. chris
    Replies:
    6
    Views:
    974
    chris
    Oct 28, 2005
  3. Skay
    Replies:
    8
    Views:
    792
    Daniel T.
    Jun 9, 2006
  4. Michel Rouzic

    Allocation of a multidimensional array of structures

    Michel Rouzic, Jun 23, 2007, in forum: C Programming
    Replies:
    14
    Views:
    615
    Michel Rouzic
    Jun 24, 2007
  5. Florian Kaufmann
    Replies:
    1
    Views:
    370
Loading...

Share This Page