unions

How to use Union in C language

馋奶兔 提交于 2019-12-05 12:12:53
I have a question about union in C Language. The variables declared in a union will share the same memory, ok, I understand. for example, union student { int i; int j; }x; how could we access the i and j? if we have: x.i = 1; and then we printf("%d",j); what will happen? compiler error? Ok then what about the following case: union student { int i; float j; }x; if we assign x.i = 2; what is the value of x.j? Assuming you use printf("%d", x.j); You will see the same value you assigned to x.i , since both variables occupy the same area of memory. It would not be typical to make both variables of

Will this bitfield work the way I expect?

北战南征 提交于 2019-12-05 12:11:37
I've been doing some reading about bitfields in C, how the C standard doesn't enforce any particular ordering of the fields in a machine word, and so on. I hope this question appropriately fits the format of SO. My question is whether my struct (definition following) will actually perform in the way I expect. Here's the definition I came up with, and then I'll discuss what I want: typedef enum { STATE_ONE, STATE_TWO, STATE_THREE, STATE_FOUR } __attribute__ ((packed)) State; typedef struct MyStruct { // One of State enum (maximum of 4 states). unsigned state : 2; // Remaining 30 bits are used

Can we access a member of a non-existing union?

你。 提交于 2019-12-05 09:32:27
In the c++ standard, in [basic.lval]/11.6 says: If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined:[...] an aggregate or union type that includes one of the aforementioned types among its elements or non-static data members (including, recursively, an element or non-static data member of a subaggregate or contained union),[...] This sentence is part of the strict-aliasing rule. Can it allow us to access the inactive member of a non existing union? As in: struct A{ int id :1; int value :32; }; struct

Can union be templated?

房东的猫 提交于 2019-12-05 09:04:07
问题 It seems unions can be templated in c++11, they are used for example in the reference implementation of std::optional. Was that possible before c++11 ? 回答1: Yes, it seems that this has always been allowed. A union is a class, and a template is either a function or a class template. Relevant parts of the standards: [temp] The declaration in a template-declaration shall — declare or define a function or a class, [...] [class] A union is a class defined with the class-key union (So one might

Union initialization in C++ and C

戏子无情 提交于 2019-12-05 08:44:39
问题 I have built a working C library, that uses constants, in header files defined as typedef struct Y { union { struct bit_field bits; uint8_t raw[4]; } X; } CardInfo; static const CardInfo Y_CONSTANT = { .raw = {0, 0, 0, 0 } }; I know that the .raw initializer is C only syntax. How do I define constants with unions in them in a way such that I can use them in C and C++. 回答1: I had the same problem. For C89 the following is true: With C89-style initializers, structure members must be initialized

Invalid union member

蓝咒 提交于 2019-12-05 08:06:09
Is there a way in Visual Studio to handle non-trivial unions. The following code is running fine using g++ -std=c++11 but VS complains: invalid union member -- class "Foo" has a disallowed member function The code is as follows: struct Foo { int value; Foo(int inV = 0) : value(inV) {} }; union CustomUnion { CustomUnion(Foo inF) : foo(inF) {} CustomUnion(int inB) : bar(inB) {} int bar; Foo foo; }; int main() { CustomUnion u(3); return 0; } Is there a way in Visual Studio to support this kind of unions (compilation option for instance)? Or should I change my code, and if so by what? I agree with

initialize a union array at declaration

夙愿已清 提交于 2019-12-05 07:19:17
I'm trying to initialize the following union array at declaration: typedef union { __m128d m; float f[4]; } mat; mat m[2] = { {{30467.14153,5910.1427,15846.23837,7271.22705}, {30467.14153,5910.1427,15846.23837,7271.22705}}}; But I'getting the following error: matrix.c: In function ‘main’: matrix.c:21: error: incompatible types in initialization matrix.c:21: warning: excess elements in union initializer matrix.c:21: warning: (near initialization for ‘m[0]’) matrix.c:21: warning: excess elements in union initializer matrix.c:21: warning: (near initialization for ‘m[0]’) matrix.c:21: warning:

Working with a union of structs in C

半世苍凉 提交于 2019-12-05 06:49:30
Say I have the following types: typedef struct TYPEA { int type; char[12] id; } TYPEA; typedef struct TYPEB { int type; int value; } TYPEB; I want to use create a union of these types and 'int', so that I can access the "type" int without needing to know whether TYPEA or TYPEB is stored in the union (the value of int lets me determine which is actually stored there). I can't get the right syntax though. My union: typedef union MEMBER { int type; struct TYPEA a; struct TYPEB b; } MEMBER; The union is accessed via: typedef struct WRAPPER { union MEMBER* member; struct WRAPPER* next; } WRAPPER;

C overcoming aliasing restrictions (unions?)

寵の児 提交于 2019-12-05 05:13:44
Assume I have a sample source file, test.c, which I am compiling like so: $ gcc -03 -Wall test.c looks something like this .. /// CMP128(x, y) // // arguments // x - any pointer to an 128-bit int // y - any pointer to an 128-bit int // // returns -1, 0, or 1 if x is less than, equal to, or greater than y // #define CMP128(x, y) // magic goes here // example usages uint8_t A[16]; uint16_t B[8]; uint32_t C[4]; uint64_t D[2]; struct in6_addr E; uint8_t* F; // use CMP128 on any combination of pointers to 128-bit ints, i.e. CMP128(A, B); CMP128(&C[0], &D[0]); CMP128(&E, F); // and so on let's also

C++ anonymous structs

感情迁移 提交于 2019-12-05 03:37:23
I use the following union to simplify byte, nibble and bit operations: union Byte { struct { unsigned int bit_0: 1; unsigned int bit_1: 1; unsigned int bit_2: 1; unsigned int bit_3: 1; unsigned int bit_4: 1; unsigned int bit_5: 1; unsigned int bit_6: 1; unsigned int bit_7: 1; }; struct { unsigned int nibble_0: 4; unsigned int nibble_1: 4; }; unsigned char byte; }; It works nice, but it also generates this warning: warning: ISO C++ prohibits anonymous structs [-pedantic] Ok, nice to know. But... how to get this warning out of my g++ output? Is there a possibility to write something like this