Discussion:
Number increments
(too old to reply)
Jack
2009-11-09 11:21:44 UTC
Permalink
Hi vc gurus,

inline float myDataObject::Get_Float()
{
float *f;
float f2;

f = (float*) bstream;

f2 = *++f;

return f2;
}

PBYTE myDataObject::bstream;

I'd like to know how to make f2 coherently increments with f? Is the equal
sign a shallow copy? From my own experiments, bstream did not increment.
Any comments are welcome!
Thanks
Jack
Jack
2009-11-09 11:25:43 UTC
Permalink
Or there is a nicer way to extract data off a stream
I would use DWORDs, floats, WORDs and strings etc
Thanks
Jack
Jack
2009-11-09 12:03:26 UTC
Permalink
Post by Jack
inline float myDataObject::Get_Float()
{
float *f;
float f2;
f = (float*) bstream;
f2 = *++f;
return f2;
}
PBYTE myDataObject::bstream;
Should be f to increment with bstream, sorry typo
Ulrich Eckhardt
2009-11-09 12:39:43 UTC
Permalink
Post by Jack
inline float myDataObject::Get_Float()
{
float *f;
float f2;
General agreement is to initialise variables at the point of their use. It
isn't necessary in C++ to declare them all at the beginning of a block.
Post by Jack
f = (float*) bstream;
Wrong, don't use C-style casts. Further, nothing guarantees that a byte
buffer is aligned correctly.
Post by Jack
f2 = *++f;
return f2;
}
PBYTE myDataObject::bstream;
I'd like to know how to make f2 coherently increments with f? Is the equal
sign a shallow copy? From my own experiments, bstream did not increment.
Well, bstream isn't touched anywhere, so yes, it shouldn't change its value.

Suggestion:

template<typename ValueType>
ValueType
get()
{
ValueType res;
memcpy(&res, bstream, sizeof res);
bstream += sizeof res;
return res;
}

Of course, this suffers a few problems still, which you may or may not care
about:
1. Endianess.
2. Different layouts of floating point numbers.
3. Different sizes of integers.
4. Buffer overruns since the access to 'bstream' is not checked in any way.
5. It first reads a value and then increments the current position, while
your code first increments and then reads. I just assumed that that was a
mistake in your code.

Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
Jack
2009-11-10 08:10:42 UTC
Permalink
Thanks, Ulrich. Let me try that out.
Jack

Loading...