Notes
C Plus Plus
C Plus Plus
  • Roadmap
    • ๐Ÿ›ฃ๏ธC Plus Plus: Docs
  • Build
    • ๐Ÿ”ฅBuild Process
    • ๐Ÿ”งConnect multiple C++ Files
  • Code
    • โฎ๏ธPre-Processors
      • #include
      • #define
      • #ifdef
      • #pragma
      • Predefined Macros
    • LValue and RValue
    • ๐Ÿช…Data Types
      • Enum
      • TypeDef
      • const in c++
      • extern vs inline
    • ๐ŸŽญCasting
    • ๐Ÿ”ƒOperator overloading
      • Available Operators
      • Examples of operator overloading
    • ๐Ÿ—บ๏ธNamespace
      • Namespace Example
      • Using directive
    • ๐Ÿ•ต๏ธHeader File
      • For C++ Classes
    • ๐Ÿ—๏ธStructure
      • Struct vs Class
      • Public vs Private inheritance
    • ๐ŸขClasses
      • Friend Function
      • Copy Constructor
      • Explicit Constructor
      • Move Constructor
        • Move Semantics
      • Other constructors
      • Virtual functions
      • Pure virtual function
      • Other function declaration
      • const function vs final function
  • Memory
    • ๐Ÿง Memory Introduction
    • โœจHeap and Stack
    • ๐ŸŽฏPointers
      • Dangling Pointer
      • 'this' Pointer
      • Function Pointer
      • Smart Pointers
        • Unique Pointer
        • Shared Pointer
        • Weak Pointer
      • Reference count
    • ๐Ÿ‘จโ€๐ŸญHelper function
    • ๐ŸกVector [ArrayList]
      • Custom vector, part 1
      • Custom vector, part 2
      • Custom vector, part 3
      • std::vector
    • โ™ป๏ธUnion
      • Type Punning
      • Type Punning, part 2
      • Type Punning, part 3
      • Union, part 1
      • Union, Part 2
  • Thread
    • ๐ŸงตThreading
      • std::thread
      • Detach a thread
  • Misc
    • ๐Ÿ—‚๏ธExecution Order
    • ๐Ÿง Print memory
Powered by GitBook
On this page
  1. Memory
  2. Union

Union, part 1

Union allows you to reuse a memory space for different-different data types, only one data can be stored at a time

Here is an example:

#include <iostream>

union U
{
    int a;
    double b;
};

void printMem(void *location)
{
    unsigned char const *pos = (unsigned char const *)location;

    for (int i = 0; i < 20; i++)
        printf("|%2.2x| ", pos[i]);
    printf("\n");
}

int main(int argc, char const *argv[])
{
    U u1;

    u1.a = 3;
    std::cout << "a: " << u1.a << ", b: " << u1.b << std::endl;
    printMem(&u1);
    
    u1.b = 50;
    std::cout << "a: " << &u1.a << ", b: " << &u1.b << std::endl;
    printMem(&u1);

    return 0;
}

Try it here:

Output:

a: 3, b: 6.95272e-310, size: 8
a: 0x7ffd9ee3ed90, b: 0x7ffd9ee3ed90, size: 8
|03| |00| |00| |00| |fd| |7f| |00| |00| |00| |3e| |6a| |2d| |2f| |ab| |1c| |8e| |00| |00| |00| |00| 
a: 0, b: 50, size: 8
a: 0x7ffd9ee3ed90, b: 0x7ffd9ee3ed90, size: 8
|00| |00| |00| |00| |00| |00| |49| |40| |00| |3e| |6a| |2d| |2f| |ab| |1c| |8e| |00| |00| |00| |00| 

Let' break down this example:

  1. We create a simple union with 2 data inside, a as integer and b as double.

  2. Once we store a = 3 and print it, u1's memory addess will store 3 as a integer.

  3. Now when we set b = 50 and print it, u1's memory address will store 50 as a double.

  4. And now if you try to read value of a, it will lose its value and will be overridden with byes of double (b).

  5. Here we reused same memory location as different data types.

PreviousType Punning, part 3NextUnion, Part 2

Last updated 10 months ago

โ™ป๏ธ