static cast - C typecasting uint32 to uint16 -


typedef struct a{     uint32 val1;     }a;  typedef struct b{     uint16 copy_val1; }b;    void function1(a input) {     b my_input;      my_input.copy_val1 = (uint16) input.val1; <-- clean? } 

inititally when struct designed, thought val1 contain 2 16 bit values. chose use 1 16 bit.

now changing copy_val1's type uint32 uint16 save memory. how should typecast in clean way , make sure 16 bit value val1 get's copied copy_val1?

the os vxworks in mips architecture.

simply assigning uint32 value uint16 variable, without cast, sufficient.

however, run risk of truncation. should consider checking see if val1 > uint16_max before assignment.

also note structures larger 1 or 2 machine registers, should pass pointers structures. otherwise incurr potentially large copy. note change in semantics, however.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -