CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 25 of 25
  1. #16
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: 3D array memory allocation dimensions

    If you're not interested in dynamically resizing the 3D array, then the approach I would use is to wrap the whole thing in a template.

    No new, no delete, copyable & assignable.

    Code:
    template<typename T, const size_t HEIGHT, const size_t WIDTH, const size_t DEPTH>
    class Array3d
    {
    public:
    
        T &operator ()(size_t x, size_t y, size_t z)
        {
            return (data[y + (x * WIDTH) + (z * WIDTH * HEIGHT)]);
        }
    
        const T &operator ()(size_t x, size_t y, size_t z) const
        {
            return (data[y + (x * WIDTH) + (z * WIDTH * HEIGHT)]);
        }
    
    private:
    
        T data[WIDTH * HEIGHT * DEPTH];
    };
    
    int main()
    {
        const int HEIGHT = 2;
        const int WIDTH  = 3;
        const int DEPTH  = 4;
    
        Array3d<short, HEIGHT, WIDTH, DEPTH> test;
    
        for (int z = 0; z < DEPTH; z++) //filling it with ones
        {
            for (int y = 0; y < WIDTH; y++)
            {
                for (int x = 0; x < HEIGHT; x++)
                {
                    test(x, y, z) = 1;
                }
            }
        }
    }
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  2. #17
    Join Date
    Apr 1999
    Posts
    27,449

    Re: 3D array memory allocation dimensions

    Quote Originally Posted by Kositch View Post
    Paul, so my second try of deleting array is now right (code is above)?
    Yes.

    Regards,

    Paul McKenzie

  3. #18
    Join Date
    Oct 2010
    Posts
    25

    Re: 3D array memory allocation dimensions

    Thank you for all your patience and help!

  4. #19
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: 3D array memory allocation dimensions

    If the array doesn't have to be a tree, then here is an example of a simple dynamic multiarray of any length
    Code:
    #include <iostream>
    #include <algorithm>
    #include <functional>
    #include <numeric>
    #include <vector>
    
    namespace demo
    {
    std::ostream& os = std::cout;
    
    template <typename T_, const unsigned S_>
    struct array
    {
        typedef std::vector<T_>                 dimension;
        typedef typename dimension::size_type   size_type;
        typedef size_type initializer_list[S_];
    
        template <typename Initializer_>
        explicit array(const Initializer_& i, const T_& v = T_())
            :
            m_dim(i, i + S_),
            m_array(std::accumulate(m_dim.begin(), m_dim.end(),
                                    1, std::multiplies<size_type>()), v)
        {
        }
    
        // example methods
        array& operator=(const size_type i)
        {
            m_array.at(calculate_pos()) = i;
            return *this;
        }
    
        array& operator[](const size_type i)
        {
            m_seq.push_back(i);
            return *this;
        }
    
        operator T_&()
        {
            return m_array.at(calculate_pos());
        }
    
        // others ...
    
    private:
        const size_type calculate_pos()
        {
            dimension i(m_seq);
            m_seq.clear();
            return std::inner_product(i.begin(), i.end(), m_dim.begin(), 0,
                              std::plus<size_type>(), std::modulus<size_type>());
        }
    
        dimension m_seq;
        dimension m_dim;
        dimension m_array;
    };
    
    
    } // end namespace
    
    int main()
    {
        {
            using namespace demo;
    
            array<int, 7>::initializer_list a = {4, 2, 3, 5 ,3, 2, 2};
    
            // pass a plain array,
            // or overload ctor to use [][][]... intiailizer syntax
            array<int, 7> arr(a, 666);
    
            arr[0][0][0][0][0][0][0] = 100;
            arr[3][1][2][4][2][1][1] = 1000;
    
            os << arr[0][0][0][0][0][0][0] << " "
               << arr[2][1][1][4][2][0][0];
        }
    }
    EDIT:
    No range checking has been provided on this example!

    EDIT2:
    calculate_pos() looks wrong but too sleepy to go over it now xD
    Last edited by potatoCode; October 11th, 2010 at 03:26 PM. Reason: added missing header

  5. #20
    Join Date
    Jan 2006
    Posts
    8

    Re: 3D array memory allocation dimensions


  6. #21
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: 3D array memory allocation dimensions

    Bah!
    Laying in bed this morning I realised that my code did nothing more than a straight array declaration! My solution was much better suited to a 3D array sized at runtime.
    Never code when half asleep

    Code:
    template<typename T>
    class Array3d
    {
    public:
    
        Array3d(size_t height, size_t width, size_t depth)
            : HEIGHT(height),
              WIDTH(width),
              DEPTH(depth),
              p_data(new T[height * width * depth])
        {
        }
    
        ~Array3d()
        {
            delete[] p_data;
        }
    
        T &operator ()(size_t x, size_t y, size_t z)
        {
            return (data[y + (x * WIDTH) + (z * WIDTH * HEIGHT)]);
        }
    
        const T &operator ()(size_t x, size_t y, size_t z) const
        {
            return (data[y + (x * WIDTH) + (z * WIDTH * HEIGHT)]);
        }
    
    private:
    
        T p_data;
        const size_t HEIGHT;
        const size_t WIDTH;
        const size_t DEPTH;
    };
    
    int main()
    {
        const int HEIGHT = 2;
        const int WIDTH  = 3;
        const int DEPTH  = 4;
    
        Array3d<short> test(HEIGHT, WIDTH, DEPTH);
    
        for (int z = 0; z < DEPTH; z++) //filling it with ones
        {
            for (int y = 0; y < WIDTH; y++)
            {
                for (int x = 0; x < HEIGHT; x++)
                {
                    test(x, y, z) = 1;
                }
            }
        }
    }
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  7. #22
    Join Date
    Sep 2010
    Posts
    31

    Re: 3D array memory allocation dimensions

    That is a pretty nice example, but you probably want to take the filling code and make it into a public method fill(const T& fill_value);

    And change
    Code:
    private:
        T p_data;
    to
    Code:
    private:
        T* p_data;
    for it to work.

  8. #23
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: 3D array memory allocation dimensions

    Quote Originally Posted by Xupicor View Post
    That is a pretty nice example, but you probably want to take the filling code and make it into a public method fill(const T& fill_value);
    That depends whether you want to follow the STL convention of making algorithms separate from the containers they work on, or not.

    And change
    Code:
    private:
        T p_data;
    to
    Code:
    private:
        T* p_data;
    for it to work.
    Doh! That what comes of posting at 6:30 in the morning, before the first mug of tea
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  9. #24
    Join Date
    Sep 2010
    Posts
    31

    Re: 3D array memory allocation dimensions

    Quote Originally Posted by JohnW@Wessex View Post
    That depends whether you want to follow the STL convention of making algorithms separate from the containers they work on, or not.
    Fair enough.

    Quote Originally Posted by JohnW@Wessex View Post
    Doh! That what comes of posting at 6:30 in the morning, before the first mug of tea
    Happens to the best of us... In untested code that is. But tea? Really? Hm, you made me want to drink one instead of the usual coffee. You malicious... fellow programmer! x]

  10. #25
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: 3D array memory allocation dimensions

    Quote Originally Posted by Xupicor View Post
    But tea? Really?
    Yes, Earl Grey.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

Page 2 of 2 FirstFirst 12

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