Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > difference of POD struct array and flat array

Reply
Thread Tools

difference of POD struct array and flat array

 
 
Peter Steiner
Guest
Posts: n/a
 
      11-03-2005
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

 
Reply With Quote
 
 
 
 
Valentin.Samko
Guest
Posts: n/a
 
      11-03-2005

> 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

 
Reply With Quote
 
 
 
 
Peter Steiner
Guest
Posts: n/a
 
      11-03-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM
Is array of POD still a POD type? Ajax Chelsea C++ 1 12-01-2003 01:56 PM
A (probable) error in perltoot ( perl5/5.8.0/pod/perltoot.pod, line number 756 ) Himanshu Garg Perl Misc 1 09-21-2003 03:28 AM



Advertisments