C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
struct vs union Posted by gameman1a on 27 Apr 2001 at 8:56 PM
Please explain the difference between a struct and a union in a demo program.

Thank you!<br>Gamemania

Planet of Wonders ( http://tech-war.virtualave.net/ )

Report
Re: struct vs union Posted by Null and Void on 27 Apr 2001 at 9:14 PM
: Please explain the difference between a struct and a union in a demo program.
:
: Thank you!Gamemania
:
: Planet of Wonders ( http://tech-war.virtualave.net/ )
:
Unions all share the same space in memory. Writing to one of its members could overwrite one or all of the other members. A union is as big as its biggest member. For example (I assume you're using a compiler/whatever that has the sizes I'm used to for its variables):
union {
  double D; // 64 bits
  float F; // 32 bits
  short S; // 16 bits
  unsigned char C; // 8 bites
} MyUnion;

int Size = sizeof(MyUnion); // Size == 64


If you set MyUnion.D equal to 50.2 then it would overwrite all of the other variables.

A struct has each variable packeted into a single block of memory (not always consecutively though). So, if we did this:
struct {
  double D; // 64 bits
  float F; // 32 bits
  short S; // 16 bits
  unsigned char C; // 8 bites
} MyStruct;

int Size = sizeof(MyStruct); // Size == 120
// I assume no byte alignment is done to the struct

Obviously writing to one member of the struct doesn't overwrite the others (under normal circumstances). So they all have their own memory and don't share it.

http://druidgames.cjb.net


Report
Re: struct vs union Posted by gameman1a on 29 Apr 2001 at 7:14 AM
Do you guys use unions often in your programs?


: : Please explain the difference between a struct and a union in a demo program.
: :
: : Thank you!Gamemania
: :
: : Planet of Wonders ( http://tech-war.virtualave.net/ )
: :
: Unions all share the same space in memory. Writing to one of its members could overwrite one or all of the other members. A union is as big as its biggest member. For example (I assume you're using a compiler/whatever that has the sizes I'm used to for its variables):
:
: union {
:   double D; // 64 bits
:   float F; // 32 bits
:   short S; // 16 bits
:   unsigned char C; // 8 bites
: } MyUnion;
: 
: int Size = sizeof(MyUnion); // Size == 64
: 

:
: If you set MyUnion.D equal to 50.2 then it would overwrite all of the other variables.
:
: A struct has each variable packeted into a single block of memory (not always consecutively though). So, if we did this:
:
: struct {
:   double D; // 64 bits
:   float F; // 32 bits
:   short S; // 16 bits
:   unsigned char C; // 8 bites
: } MyStruct;
: 
: int Size = sizeof(MyStruct); // Size == 120
: // I assume no byte alignment is done to the struct
: 

: Obviously writing to one member of the struct doesn't overwrite the others (under normal circumstances). So they all have their own memory and don't share it.
:
: http://druidgames.cjb.net
:
:

Gamemania

Planet of Wonders ( http://tech-war.virtualave.net/ )


Report
Re: struct vs union Posted by Null and Void on 29 Apr 2001 at 9:54 AM
No, not often, but I have before, and other libraries (DirectX, WinSock2, et cetera) use them, so they're good to know.
http://druidgames.cjb.net





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.