Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Difference b/w Unions and Structures...

Difference b/w Unions and Structures...

Scheduled Pinned Locked Moved C / C++ / MFC
question
9 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xelios
    wrote on last edited by
    #1

    What is the difference b/w Unions and Structures...

    Mohsin Ali

    A C T H S 5 Replies Last reply
    0
    • X xelios

      What is the difference b/w Unions and Structures...

      Mohsin Ali

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      Basically the members of a union share the same memory address (while the members of a struct have distinct memory addresses). The above implies, for instance:

      union MyUnion
      {
      int i;
      float f;
      };

      struct MyStruct
      {
      int i;
      float f;
      };

      void main()
      {
      MyUnion u;
      MyStruct s;
      u.i = 5; // this also changes u.f
      s.i = 5; // this doesn't change s.f
      }

      and, conceptually, sizeof(MyStruct)=sizeof(int)+sizeof(float) while sizeof(MyUnion)= max(sizeof(int), sizeof(float)) :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

      1 Reply Last reply
      0
      • X xelios

        What is the difference b/w Unions and Structures...

        Mohsin Ali

        A Offline
        A Offline
        Alan Balkany
        wrote on last edited by
        #3

        With unions all members start at the same base address so they overlap. With structures, the members are "stacked", i.e. each one starts in the byte after the previous one ends.

        D 1 Reply Last reply
        0
        • X xelios

          What is the difference b/w Unions and Structures...

          Mohsin Ali

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          question before answering : did you bother searching the web/docs/msdn/google/whatever before asking that dumb question asked and answered tons of times ? the difference between unions and structures (and classes if in C++) is that all the members of the union share the same memory address. in other words, it means that they all start at the same address, and them, it also means that the size of the union equals the size of its largest member. so, the other question which usually comes after such an answer is : "but what can it be used for, then ?". good question Sherlock. in fact, it's not used much in everyday programming, especially in management applications. I used to use it when I was coding for hardware, when some component had to share the same physical address. so to represent such a material in the software, a union was exactly what was needed.

          [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

          1 Reply Last reply
          0
          • X xelios

            What is the difference b/w Unions and Structures...

            Mohsin Ali

            H Offline
            H Offline
            Hamid Taebi
            wrote on last edited by
            #5

            I think you are new to c++ so I suggest to see www.cplusplus.com and on that site you can see different union and other helpful info.

            T 1 Reply Last reply
            0
            • H Hamid Taebi

              I think you are new to c++ so I suggest to see www.cplusplus.com and on that site you can see different union and other helpful info.

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #6

              [Message Deleted]

              H 1 Reply Last reply
              0
              • X xelios

                What is the difference b/w Unions and Structures...

                Mohsin Ali

                S Offline
                S Offline
                sudhir_Kumar
                wrote on last edited by
                #7

                While structure enables us treat a number of different variables stored at different in memory , a union enables us to treat the same space in memory as a number of different variables. That is a Union offers a way for a section of memory to be treated as a variable of one type on one occasion and as a different variable of a different type on another occasion. Union allocates the memory equal to the maximum memory required by the member of the union but structure allocates the memory equal to the total memory required by the members. Union allocates the memory equal to the maximum memory required by the member of the union but structure allocates the memory equal to the total memory required by the members. Example: union exforsys_t { char c; int i; float f; } exforsys; Defines three elements: exforsys.c exforsys.i exforsys.f Each one with a different data type. Since all of them are referring to the same location in memory, the modification of one of the elements will affect the value of all of them. We cannot store different values in them independent from each other.

                Sudhir Kumar

                1 Reply Last reply
                0
                • T toxcct

                  [Message Deleted]

                  H Offline
                  H Offline
                  Hamid Taebi
                  wrote on last edited by
                  #8

                  It was optional no coercion because I didnt use of link for that site. ;P

                  1 Reply Last reply
                  0
                  • A Alan Balkany

                    With unions all members start at the same base address so they overlap. With structures, the members are "stacked", i.e. each one starts in the byte after the previous one ends.

                    D Offline
                    D Offline
                    DQNOK
                    wrote on last edited by
                    #9

                    Alan Balkany wrote:

                    each one starts in the byte after the previous one ends.

                    Too specific to be the right answer for this newbie. One poster said "conceptually" the sizes of the members sum, which is correct *CONCEPTUALLY*. We should probably point out to the original poster that in a structure, the compiler is free to add padding after ANY member (including the last member) to insure the members align correctly. That is, a structure may contain unused bytes that are there only for alignment.

                    struct example{
                       char c;  //one byte?
                       long l;  //four bytes?
                    };
                    printf( "sizeof(example) = %d", sizeof(example) );
                    

                    In most compilers, this will NOT print "sizeof(example) = 5" as might be expected, since the compiler must put padding after c to insure that l aligns properly. In most modern compilers this will likely print "sizeof(example) = 8".

                    David --------- Empirical studies indicate that 20% of the people drink 80% of the beer. With C++ developers, the rule is that 80% of the developers understand at most 20% of the language. It is not the same 20% for different people, so don't count on them to understand each other's code. http://yosefk.com/c++fqa/picture.html#fqa-6.6 ---------

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups