unions

Error: Why 'void*' is not a pointer-to-object type even though the pointer is set to an object?

拈花ヽ惹草 提交于 2020-05-16 04:10:10
问题 I have the following code (live on Coliru): // untouchable extern library .hpp file typedef union ExternLibraryUnion { int a; float b; }ExternLibraryUnion; // my code #include <iostream> class Container{ public: Container() : m_union(NULL) {}; ~Container(){ if(m_union){ delete m_union; } } void init(){ m_union = new ExternLibraryUnion(); } ExternLibraryUnion* get_union(){ return m_union; } private: ExternLibraryUnion* m_union; }; class Master{ public: Master() : m_union(NULL) { m_container

Error: Why 'void*' is not a pointer-to-object type even though the pointer is set to an object?

心不动则不痛 提交于 2020-05-16 04:07:09
问题 I have the following code (live on Coliru): // untouchable extern library .hpp file typedef union ExternLibraryUnion { int a; float b; }ExternLibraryUnion; // my code #include <iostream> class Container{ public: Container() : m_union(NULL) {}; ~Container(){ if(m_union){ delete m_union; } } void init(){ m_union = new ExternLibraryUnion(); } ExternLibraryUnion* get_union(){ return m_union; } private: ExternLibraryUnion* m_union; }; class Master{ public: Master() : m_union(NULL) { m_container

Is it legal write to a byte array in a union and read from an int to convert values in MISRA C?

跟風遠走 提交于 2020-04-13 08:05:11
问题 I guess this must have been asked before, but I could not get a specific yes/no answer. I have this code snippet : union integer_to_byte { signed int IntPart; unsigned char BytePart[2]; }; typedef union integer_to_byte I2B; main() { I2B u16VarNo; while(1) { // some code.... u16VarNo.BytePart[1]= P1; // some more code .... u16VarNo.BytePart[0]= P2; // still more code ... if(u16VarNo.IntPart != 0xFFFF) { } } } Is this a legal way to use Unions in C ? From what I read; only the last assigned