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;
}

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 8 months ago

Try it :

♻️
here