Search
 
SCRIPT & CODE EXAMPLE
 

CPP

cpp unions

#include <iostream>
#include <cstdint>
union S
{
    std::int32_t n;     // occupies 4 bytes
    std::uint16_t s[2]; // occupies 4 bytes
    std::uint8_t c;     // occupies 1 byte
};                      // the whole union occupies 4 bytes
 
int main()
{
    S s = {0x12345678}; // initializes the first member, s.n is now the active member
    // at this point, reading from s.s or s.c is undefined behavior
    std::cout << std::hex << "s.n = " << s.n << '
';
    s.s[0] = 0x0011; // s.s is now the active member
    // at this point, reading from n or c is UB but most compilers define it
    std::cout << "s.c is now " << +s.c << '
' // 11 or 00, depending on platform
              << "s.n is now " << s.n << '
'; // 12340011 or 00115678
}
Comment

unions c++

A union is a user-defined type in which all members share the same memory location. 
This definition means that at any given time, a union can contain no more than one object from its list of members. 
It also means that no matter how many members a union has, it always uses only enough memory to store the largest member.

A union can be useful for conserving memory when you have lots of objects and limited memory. 
However, a union requires extra care to use correctly. 
You're responsible for ensuring that you always access the same member you 
assigned. If any member types have a non-trivial constructor, then you must
write additional code to explicitly construct and destroy that member. 
Before you use a union, consider whether the problem
you're trying to solve could be better expressed by using a 
base class and derived class types.
Comment

PREVIOUS NEXT
Code Example
Cpp :: map in c++ sorted descending order 
Cpp :: c++ sort vector 
Cpp :: run c++ program in mac terminal 
Cpp :: check if file is empty c++ 
Cpp :: string reverse stl 
Cpp :: for in c++ 
Cpp :: slice std::array cpp 
Cpp :: how to make a loop in c++ 
Cpp :: how to make a list in c++ 
Cpp :: if vector is empty c++ 
Cpp :: c++ call method in same class 
Cpp :: all data types in c++ 
Cpp :: find primes in a range in c++ 
Cpp :: how to store pair in min heap in c++ 
Cpp :: how to convert string into lowercase in cpp 
Cpp :: concatenate string program in c++ 
Cpp :: coordinate in 1d array 
Cpp :: update variable in const function C++ 
Cpp :: std distance 
Cpp :: c++ loop vector 
Cpp :: c++ how to add something at the start of a vector 
Cpp :: Accpt array input in single line in cpp 
Cpp :: case label in c++ 
Cpp :: sina + sinb formula 
Cpp :: pointer in return function c++ 
Cpp :: how to add c++14 in sublime text 
Cpp :: c++ 14 for sublime windoes build system 
Cpp :: Search Insert Position leetcode solution in cpp 
Cpp :: how to slice vector in c++ 
Cpp :: demonstrate constructor 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =