Thread: union u_type What it is?

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    119

    union u_type What it is?

    Code:
    #include <iostream>
    union u_type
    {
    u_type(short int a);
    void showchars();
    short int i;
    char ch[2];
    };
    u_type::u_type(short int a)
    {
    i = a;
    }
    void u_type::showchars()
    {
    std::cout << ch[0] << " ";
    std::cout << ch[1] << "\n";
    }
    int main()
    {
    u_type u(1000);
    u.showchars();
    return 0;
    }
    Hello!
    What it is? In my head non-selection. The author of the code charges the value in short int i ;. The value is inserted, this desire of the author is seen in the code, and indeed, the number gets where it would be desirable.
    But char ch [2]; separately, it simply lies in the union. No number was loaded into it, no symbols were inserted into it at all.
    Suddenly we print this to the console. How so? What is in it? Case values? What's going on here?
    The console displays a letter or badge, like the Ukrainian letter ш, and a heart, a card suit of hearts. Where from?

    Can my eyes today not see the elementary? Or I do not understand important principles?
    Last edited by Dmy; 05-24-2017 at 08:57 AM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    A union is like a structure, except that all data members occupy the same space in memory. However, your example exhibits undefined behavior, because you may only read from the field that was most recently written to. Most implementations define it to do the thing that you're attempting to demonstrate here, but it would be technically correct, according to the international C++ standard, to do something completely different.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Suddenly we print this to the console. How so? What is in it? Case values? What's going on here?
    Try it with say

    u_type u(0x3031);
    u_type u(0x4142);
    u_type u(0x6670);


    Different member union access is implementation defined (in C99 at least).
    Quote Originally Posted by c99
    With one exception, if the value of a member of a union object is used when the most
    recent store to the object was to a different member, the behavior is
    implementation-defined. 70) 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
    completed 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Salem View Post
    Different member union access is implementation defined (in C99 at least).
    In C++, according to Union declaration - cppreference.com, it is undefined. I'm looking for the specific section of the standard, but cppreference is usually pretty accurate.
    Last edited by Elkvis; 05-24-2017 at 02:13 PM.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User
    Join Date
    Nov 2015
    Posts
    119
    Many thanks! I am very thankful you that you clarify my questions!


    DropMeFiles – free one-click file sharing service
    Last edited by Dmy; 05-26-2017 at 01:24 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c- union
    By Ragu Raman in forum C Programming
    Replies: 3
    Last Post: 06-27-2013, 07:05 PM
  2. Union of a set
    By bman900 in forum C Programming
    Replies: 5
    Last Post: 11-07-2010, 02:46 AM
  3. union
    By shiju in forum C Programming
    Replies: 2
    Last Post: 01-22-2004, 01:52 AM
  4. What is a union?
    By ammar in forum C++ Programming
    Replies: 1
    Last Post: 11-17-2002, 03:36 AM
  5. Union
    By {Dnw} in forum C Programming
    Replies: 7
    Last Post: 08-18-2001, 12:28 AM

Tags for this Thread