Thread: How union occupy memory

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    90

    How union occupy memory

    I have been through various tutorials but I do not understand how union member occupy memory

    Code:
    #include <stdio.h>
    union example
    {
        int x;
        char y;
        float z;
    };
    
    int main()
    {  
        union example v;
    
        v.x = 2;
        v.y ='A';
        v.z = 13.43;
        printf("v.x = %d \n", sizeof(v.x));
        printf("v.x = %d \n", sizeof (v.y));
        printf("v.x = %d \n", sizeof(v.z));
        printf("v = %d \n", sizeof(v))
    
        return 0; }
    int variable takes four bytes, char variable take one byte and float take four byte

    anybody can tell me How much memory union occupy to store three union members
    Last edited by Player777; 04-08-2020 at 02:18 AM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I suggest you learn how to compile and run programs. And then you would know the answers to your question!

    Edit: This "float take one byte" is wrong.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    90
    Quote Originally Posted by stahta01 View Post
    I suggest you learn how to compile and run programs. And then you would know the answers to your question!

    Edit: This "float take one byte" is wrong.

    Tim S.
    Code:
    #include <stdio.h>
    
    union example
    {
        int x;
        char y;
        float z;
    };
    
    
    int main()
    {  
        union example v;
    
    
        v.x = 2;
        v.y ='A';
        v.z = 13.43;
        printf("v.x = %d \n", sizeof(v.x));
        printf("v.y = %d \n", sizeof (v.y));
        printf("v.z = %d \n", sizeof(v.z));
        printf("v = %d \n", sizeof(v));
    
    
        return 0;
        
        }
    Output
    v.x = 4
    v.y = 1
    v.z = 4
    v = 4

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Another recent thread on union confusion.
    why the result of print statement for a.x isn''t 1?

    > anybody can tell me How much memory union occupy to store three union members
    A union is typically the size of it's largest member.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Underallocating memory for a tagged union.
    By Michael Baker in forum C Programming
    Replies: 6
    Last Post: 04-06-2015, 12:23 AM
  2. union will support dynamic memory allocation?
    By nkrao123@gmail. in forum C Programming
    Replies: 8
    Last Post: 11-24-2011, 09:56 AM
  3. Occupy cprogamming.com!
    By MK27 in forum General Discussions
    Replies: 49
    Last Post: 11-03-2011, 01:45 PM
  4. Memory allocation in an UNION
    By NKP in forum C Programming
    Replies: 6
    Last Post: 06-13-2010, 10:58 PM
  5. Replies: 12
    Last Post: 12-09-2009, 12:49 PM

Tags for this Thread