unions

Is it safe to detect endianess with union?

微笑、不失礼 提交于 2019-12-01 03:39:31
In other words, according to the C standard , is this code safe? (Assume uint8_t is one byte) void detectEndianness(void){ union { uint16_t w; uint8_t b; } a; a.w = 0x00FFU; if (a.b == 0xFFU) { puts("Little endian."); } else if (a.b == 0U) { puts("Big endian."); } else { puts("Stack Overflow endian."); } } What if I change it into this? Note the third if case that I'm aware of. a.w = 1U; if (a.b == 1U) { puts("Little endian."); } else if (a.b == 0U) { puts ("Big endian."); } else if (a.b == 0x80U) { /* Special potential */ } else { puts("Stack Overflow endian."); } Quoting from n1570: 6.5.2.3

Clarification on an example of unions in C11 standard

不打扰是莪最后的温柔 提交于 2019-12-01 03:39:30
The following example is given in the C11 standard, 6.5.2.3 The following is not a valid fragment (because the union type is not visible within function f): struct t1 { int m; }; struct t2 { int m; }; int f(struct t1 *p1, struct t2 *p2) { if (p1->m < 0) p2->m = -p2->m; return p1->m; } int g() { union { struct t1 s1; struct t2 s2; } u; /* ... */ return f(&u.s1, &u.s2); } Why does it matter that the union type is visible to the function f? In reading through the relevant section a couple times, I could not see anything in the containing section disallowing this. It matters because of 6.5.2.3

Using std::launder to get a pointer to an active object member from a pointer to an inactive object?

时光怂恿深爱的人放手 提交于 2019-12-01 03:18:24
This question followes this one Let's consider this example code: struct sso { union{ struct { char* ptr; char size_r[8]; } large_str; char short_str[16]; }; bool is_short_str() const{ return *std::launder(short_str+15)=='\0'; //UB? } }; If short_str is not the active member dereferencing the pointer without std::launder would be UB. Let's consider that the ABI is well specified and that we know that size_r[7] is at the same address as short_str[15]. Does std::launder(short_str+15) return a pointer to size_r[7] when short_str is not the active member of the union? Nota: I think this is the

Anonymous union and struct [duplicate]

梦想的初衷 提交于 2019-12-01 02:32:21
This question already has an answer here: Why does C++ disallow anonymous structs? 6 answers How would you go about doing this in standard C++11/14 ? Because if I'm not mistaken this isn't standard compliant code with the anonymous structs. I wish to access the members the same way as you would with this. template <typename some_type> struct vec { union { struct { some_type x, y, z; }; struct { some_type r, g, b; }; some_type elements[3]; }; }; Praetorian Yes, neither C++11 nor C++14 allow anonymous structs. This answer contains some reasoning why this is the case. You need to name the structs

Why aligning of long long union member is bigger than the containing union/struct? Is this correct?

旧巷老猫 提交于 2019-12-01 00:27:12
问题 From this question one could start to believe that alignment of a union is not less than the largest alignment of it's individual members. But I have a problem with the long long type in gcc/g++. The full example can be found here, but here are the relevant parts for my question: union ull { long long m; }; struct sll { long long m; }; int main() { #define pr(v) cout << #v ": " << (v) << endl pr(sizeof(long long)); pr(__alignof__(long long)); pr(sizeof(ull)); pr(__alignof__(ull)); pr(sizeof

Why this union's size is 2 with bitfields?

狂风中的少年 提交于 2019-11-30 23:56:16
I am working on turbo C on windows where char takes one byte.Now my problem is with the below union. union a { unsigned char c:2; }b; void main() { printf("%d",sizeof(b)); \\or even sizeof(union a) } This program is printing output as 2 where as union should be taking only 1 byte. Why is it so? for struct it is fine giving 1 byte but this union is working inappropriately. And one more thing how to access these bit fields. scanf("%d",&b.c); //even scanf("%x",b.c); is not working because we cannot have address for bits.So we have to use another variable like below int x; scanf("%d",&x); b.c=x;

Using std::launder to get a pointer to an active object member from a pointer to an inactive object?

送分小仙女□ 提交于 2019-11-30 23:25:24
问题 This question followes this one Let's consider this example code: struct sso { union{ struct { char* ptr; char size_r[8]; } large_str; char short_str[16]; }; bool is_short_str() const{ return *std::launder(short_str+15)=='\0'; //UB? } }; If short_str is not the active member dereferencing the pointer without std::launder would be UB. Let's consider that the ABI is well specified and that we know that size_r[7] is at the same address as short_str[15]. Does std::launder(short_str+15) return a

Could someone explain this C++ union example?

空扰寡人 提交于 2019-11-30 23:24:17
I found this code on cppreference.com. It's the strangest C++ I've seen, and I have a few questions about it: union S { std::string str; std::vector<int> vec; ~S() {} }; int main() { S s = { "Hello, world" }; // at this point, reading from s.vec is undefined behavior std::cout << "s.str = " << s.str << '\n'; s.str.~basic_string<char>(); new (&s.vec) std::vector<int>; // now, s.vec is the active member of the union s.vec.push_back(10); std::cout << s.vec.size() << '\n'; s.vec.~vector<int>(); } I want to make sure I've got a few things right. The union forces you to initialise one of the union

Is it safe to detect endianess with union?

拟墨画扇 提交于 2019-11-30 23:23:49
问题 In other words, according to the C standard , is this code safe? (Assume uint8_t is one byte) void detectEndianness(void){ union { uint16_t w; uint8_t b; } a; a.w = 0x00FFU; if (a.b == 0xFFU) { puts("Little endian."); } else if (a.b == 0U) { puts("Big endian."); } else { puts("Stack Overflow endian."); } } What if I change it into this? Note the third if case that I'm aware of. a.w = 1U; if (a.b == 1U) { puts("Little endian."); } else if (a.b == 0U) { puts ("Big endian."); } else if (a.b ==

Exception to strict aliasing rule in C from 6.5.2.3 Structure and union members

妖精的绣舞 提交于 2019-11-30 21:15:32
Quote from C99 standard: 6.5.2.3 5 One special guarantee is made in order to simplify the use of unions: if a union contains several structures that share a common initial sequence (see below), and if the union object currently contains one of these structures, it is permitted to inspect the common initial part of any of them anywhere that a declaration of the complete type of the union is visible. Two structures share a common initial sequence if corresponding members have compatible types (and, for bit-fields, the same widths) for a sequence of one or more initial members. There is example