473,378 Members | 1,280 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

difference of POD struct array and flat array

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

Nov 3 '05 #1
2 2282
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

Nov 3 '05 #2
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

Nov 3 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Shea Martin | last post by:
I have been programming in C++ for over 4 years. I *think* I knew that a struct could have a constructor but I decided to dig into it a little more today, and found that there is very little...
15
by: Mohanasundaram | last post by:
Hi All, What is the difference between malloc and calloc other than the point that calloc will initialize the memory to all zeros? This was an interview question for me. All the books and...
79
by: Me | last post by:
Just a question/observation out of frustration. I read in depth the book by Peter Van Der Linden entitled "Expert C Programming" (Deep C Secrets). In particular the chapters entitled: 4: The...
5
by: Kobu | last post by:
In embedded systems (programmed in C), often times structure declarations are used to group together several status/control/data registers of external hardware (or even internal registers). The...
56
by: ccwork | last post by:
Hi all, Here is a sample code segment: .... typedef PACKED struct { union { PACKED struct { char red:1;
4
by: TG | last post by:
Hi there ! I'm just starting to use Numeric here, and I'm wondering : how can I efficiently initialize every values of a N-dimensional array, given I don't know the number of dimensions ? I'm...
18
by: Ehud Shapira | last post by:
Is it possible to have a declaration of a struct pointer initialized to an unnamed struct? (I'm only concerned with static/global variables, if it matters.) I'm trying to do something like: ...
45
by: anto frank | last post by:
hi friends, is ther any difference in array in c and array in c++?
3
by: eso40043 | last post by:
Hi guys, I've asked for help here once before with great results, I thought I'd try it again. (I'm on Ubuntu 6.06 with python 2.4. Xemacs as editor.) I have an image as a scipy array and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.