c++ - Cast integral array to class array -


i'm using library passes me pointer array of int32_t. need math on these numbers, data in q23.8 format.

i created class overloading basic math operators, i'm stuck how cast int32_t array pointer new class -- datatypes different sizes. there way force new class match size of int32_t, or there way done?

note: want avoid copying each item of large array, i'm developing resource constrained system.

first of all, please note if not constrained performance, memory consumption, solution suggested @aumnayan may preferable simplicity , portability.

in order force objects match size int32_t , alignment of q23.8 can use bit-fields:

struct q_23_8 {     int32_t fractional :  8;     int32_t integral   : 23; };  static_assert(sizeof(q_23_8) == sizeof(int32_t), "sizes differ!"); 

please note order of fields depends on target cpu endianness; sample little-endian. also, ensure size of class may need use compiler's packing , alignment control capabilities (e g. #pragma pack).

having int32_t encoded[] can interpret array of q_23_8 follows:

q_23_8* decoded = reinterpret_cast<q_23_8*>(encoded); 

here complete code sample: http://ideone.com/x177fq. suggest reading more bit-fields, portability, , limitations.


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -