On 10/15/11 7:32 PM, someone wrote: ....
Last question for today and I shall soon close the thread: Can I ask
you which of the following you think is the best to use (or suggest
alternatives)?
if (0) // my way of switching between alternate "solutions" whenI
want both in the code
R1_begin = *(reinterpret_cast<unsigned char*> (&memblock[0]));//,
1, 'uint32',endianNess);
else
memcpy(&R1_begin,&memblock[0], sizeof(R1_begin));
The first doesn't do what you want. &memblock[0] is already a char* so
casting it to unsigned char* and dereferencing isn't going to make a big
change. If you did a cast to unsigned int*, that would be different.
Ok, it was probably a coincidence that it worked... It sounds right,
what you write. Thanks.
You also shouldn't use reinterpret_cast here, static_cast is good
enough, and is actually what you want.
I ended up doing this (which I find is quite good, I stole some part
of the idea from the internet):
//=================================
template <class T>
void moveBytes(T *destination, const char *source, const unsigned int
multiplier=1)
{
// cout << "sizeof(*destination) = " << dec << sizeof(*destination)
<< endl;
memcpy(destination, &source[memLocation],
(multiplier*sizeof(*destination)));
memLocation += (multiplier*sizeof(*destination)); // memLocation =
global variable/counter!
}
//=================================
This allows me to extract different data types from memory and do,
e.g.:
unsigned int R1_begin;
moveBytes(&R1_begin, memblock); // memLocation automatically
increases for next time
unsigned int * R2_SensNum;
R2_SensNum = new unsigned int[R2_N];
moveBytes(R2_SensNum, memblock, R2_N);
etc... And I think it works for all data types: floats, doubles,
uint32, int32, whatever...
The memcpy is better, as the cast has the danger that some fields might
not line up on the right word boundaries in your data buffer. It also
says you can later change them to the network order calls later to make
your program more portable.
Ok, memcpy it is
Now, I have one last serious issue with my program before it works...
I sucessfully loaded all the "header"-information by using the
moveBytes-template (see above). After the header, follows a long loop
of consecutive data records so I do something like this (short
version, some irrelevant code parts is omitted):
//=================================
RTSensor_array = new float[R2_N]; // should be array of size R2_N!
vector<float> RTSensorReading[R2_N]; // R2_N = 10 (this is the sensor
array length)
while ( memLocation < fsize ) // fsize was the filesize, memLocation
is also seen above (global)
{
curLoopNumber++; // keep track of number of records (for debugging)
moveBytes(&myuint_begin, memblock); // this works, read 4 bytes!
moveBytes(&float_time, memblock); // this works, read 4 bytes!
moveBytes(&myuint_end, memblock); // this works, read 4 bytes!
moveBytes(&RTSensor_array, memblock); // issue: much longer than 4
bytes, i.e. 40++ bytes
RTbegin.push_back( myuint_begin ); // this works, save 4 bytes!
RTtime.push_back( float_time ); // this works, save 4 bytes!
RTend.push_back( myuint_end ); // this works, save 4 bytes!
RTSensorReading.push_back( RTSensor_array ); // <==== ISSUE, not
working, trying to save 40 bytes!
}
//=================================
The last line gives this compiler error:
error: request for member ‘push_back’ in ‘RTSensorReading’, which is
of non-class type ‘std::vector<float> [(((long unsigned int)(((long
int)R2_N) + -0x00000000000000001)) + 1)]’
So... In order to make it more clear: For each time-step (e.g.
time={0,1,2,3,4...} seconds), maybe 10 or 20 sensors values is stored.
Because float is 4 bytes, let's assume that I want to read 10 sensors,
e.g. RTSensor_array = 10x4 = 40 bytes. For each consecutive data
record, I have some temporary variables, i.e. the first 4 moveBytes-
lines: unsigned int myuint_begin, float float_time, unsigned int
myuint_end, vector<float> RTSensor_array.
My question: What is the most efficient way of storing this float
array using C++ ?
Maybe later I want to access sensor number 4 (index 4 in the
RTSensor_array) as a function of the time which is saved in
vector<float> RTtime;
I hope the question is understandable and else I'll try to explain it
better... The vector issue is quite essential to C++ so I hope I get
some helpful answers/hints which I can learn a great deal from. Thanks
so far!