• Skip to main content
  • Skip to search
  • Skip to footer
Cadence Home
  • This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

  1. Community Forums
  2. Functional Verification
  3. How/Where/when to use unions

Stats

  • Locked Locked
  • Replies 1
  • Subscribers 64
  • Views 2420
  • Members are here 0
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How/Where/when to use unions

hipooja
hipooja over 15 years ago

I understand union to be a user defined data type that can store variables of different data types,however unlike class they do not contain methods to operate on the properties and they occupy the same memory as the largest data in them.

I however do not know How ,when ,where and why to use unions as i have used classes extensively but not tried my hand at unions

I wrote a simple code ,my idea was to display the properties of the union num ,defined below.i also want to know if unions and structs have a constructor like classes?

program p1;
typedef union packed {

int i;
real f;
} num ;

num n1 , n2;
n1.f = 0.5;
n2.i = -44;
$display("f = %h\n",n1.f);
endprogram

 

I declareed the union to be packed as it otherwise issued an error that unpacked unions are unsupported,Secondly cant we define unions independently ,the way we define classes? I get a syntacx error at n1.f = 0.5 ,can you help me understand and use unions better?

 

Regards,

Pooja

  • Cancel
Parents
  • Vinayhonnavara
    Vinayhonnavara over 15 years ago

    Hi Pooja,

    Basically you use unions to have different interpretations of a single daya type.

     

    Please check the following code, i have not used packed here and unpacked will work here. You missed the initial begin-end block. Other than that your code is flawless.

     program p1;
    typedef union packed {
    int i;
    real f;
    } num ;

    num n1 , n2;
    initial begin
    n1.f = 0.5;
    n2.i = -44;
    $display("f = %f\n",n1.f);
    end
    endprogram

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
Reply
  • Vinayhonnavara
    Vinayhonnavara over 15 years ago

    Hi Pooja,

    Basically you use unions to have different interpretations of a single daya type.

     

    Please check the following code, i have not used packed here and unpacked will work here. You missed the initial begin-end block. Other than that your code is flawless.

     program p1;
    typedef union packed {
    int i;
    real f;
    } num ;

    num n1 , n2;
    initial begin
    n1.f = 0.5;
    n2.i = -44;
    $display("f = %f\n",n1.f);
    end
    endprogram

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
Children
No Data

Community Guidelines

The Cadence Design Communities support Cadence users and technologists interacting to exchange ideas, news, technical information, and best practices to solve problems and get the most from Cadence technology. The community is open to everyone, and to provide the most value, we require participants to follow our Community Guidelines that facilitate a quality exchange of ideas and information. By accessing, contributing, using or downloading any materials from the site, you agree to be bound by the full Community Guidelines.

© 2025 Cadence Design Systems, Inc. All Rights Reserved.

  • Terms of Use
  • Privacy
  • Cookie Policy
  • US Trademarks
  • Do Not Sell or Share My Personal Information