difference of POD struct array and flat array

Discussion in 'C++' started by Peter Steiner, Nov 3, 2005.

  1. hi there!

    my problem: i need arrays of float-triples (math vector). i need these
    available in a flat float* array for batch processing (opengl). is an
    array of POD structs with three float members each equivalent to a flat
    array of floats in terms of memory layout?

    ie. is the following code legal?


    struct Vector {
    float v[3];
    };

    Vector vectors[3];
    float flatVectors[3][3];

    memcpy((const void*)flatVectors, (void*)vectors, sizeof(float)*9);
    // vectors now contains the same values in Vector::v as flatVectors


    i suspect that struct member alignment, as applied by compilers,
    forbids that kind of usage. is that true, and if so, is there a way to
    circumvent that problem in a portable fashion?

    i am interested in such a solution because i would like to implement a
    convenient math vector class in a platform-independend, purely object
    oriented program.

    -- peter
     
    Peter Steiner, Nov 3, 2005
    #1
    1. Advertising


  2. > struct Vector {
    > float v[3];
    > };
    >
    > Vector vectors[3];
    > float flatVectors[3][3];
    >
    > memcpy((const void*)flatVectors, (void*)vectors, sizeof(float)*9);


    > i suspect that struct member alignment, as applied by compilers,
    > forbids that kind of usage.

    Yes. sizeof(Vector) does not have to be equal to sizeof(float)*3.

    > is that true, and if so, is there a way to
    > circumvent that problem in a portable fashion?


    Write a wrapper around the raw float array. Something like

    template<class T, size_t vector_size>
    class Matrix
    {
    typedef std::vector<T> container;

    template<class U, class SizeType, class Iterator>
    struct VectorT
    {
    typedef SizeType size_type;
    typedef U value_type;
    VectorT() {}
    value_type& operator[] (size_type pos) const { return * (ptr_ + pos);
    }
    private:
    VectorT(const VectorT&);
    VectorT& operator=(const VectorT&);
    VectorT(Iterator ptr) : ptr_(ptr) {}
    friend class Matrix;
    Iterator ptr_;
    };
    public:

    typedef typename container::size_type size_type;
    typedef VectorT<T, size_type, typename container::iterator>
    vector_proxy;
    typedef VectorT<const T, size_type, typename
    container::const_iterator> const_vector_proxy;

    vector_proxy operator[](size_t pos) { return vector_proxy(c_.begin() +
    (vector_size * pos)); }
    const_vector_proxy operator[](size_t pos) const { return
    const_vector_proxy(c_.begin() + (vector_size * pos)); }

    T* get_raw_data_for_opengl() { return &c_[0]; }

    private:
    container c_;
    };

    Or you could even implement a full blown STL compatible matrix like
    container with iterators.

    --

    Valentin Samko - http://www.valentinsamko.com
     
    Valentin.Samko, Nov 3, 2005
    #2
    1. Advertising

  3. thanks for the elegant solution!

    i'll go for this approach, which gives me the comfort with the proxy
    class and the safety by wrapping the container of raw values. win-win.
    :)

    -- peter
     
    Peter Steiner, Nov 3, 2005
    #3
    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. Ajax Chelsea

    Is array of POD still a POD type?

    Ajax Chelsea, Dec 1, 2003, in forum: C++
    Replies:
    1
    Views:
    346
    Ron Natalie
    Dec 1, 2003
  2. Ivan A. Kosarev

    Operator new and POD-struct

    Ivan A. Kosarev, Dec 1, 2005, in forum: C++
    Replies:
    6
    Views:
    328
  3. Hicham Mouline

    struct POD and return-by-value

    Hicham Mouline, Jun 3, 2009, in forum: C++
    Replies:
    2
    Views:
    285
    Victor Bazarov
    Jun 3, 2009
  4. Himanshu Garg
    Replies:
    1
    Views:
    166
    Himanshu Garg
    Sep 21, 2003
  5. Tuan  Bui
    Replies:
    14
    Views:
    476
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page