Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 8th, 2006, 11:49 PM   #1
Harakim
Hobbyist Programmer
 
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3 Harakim is on a distinguished road
What are unions?

What are unions?

I want to make a union like...
Union u { char *cptr; short *sptr; int *iptr; }

Then I would like to use it scroll through a list with those pointers, so I don't have to keep casting. Is this possible and is there a better way to do it?
Harakim is offline   Reply With Quote
Old Jun 9th, 2006, 6:08 AM   #2
volatile
Newbie
 
Join Date: Nov 2005
Posts: 18
Rep Power: 0 volatile is on a distinguished road
Why do you want to make a union when you don't know what its for?

A union is a data structure wich size is only as big as its biggest member, let me clarify:

#include <iostream>

using namespace std;

int main()
{
	union { char a; int b; } ;
	
	a = 'a';
	cout << "a is " << a << endl;
	b = 0x99887762;
	
	cout << "a is " << a << endl;
	cout << "b is " << hex << b << endl;
	
	a = 'A';
	cout << "a is " << a << endl;
	cout << "b is " << hex << b << endl;
	
	cin.get();
	return EXIT_SUCCESS;
}
Output:
a is a
a is b
b is 99887762
a is A
b is 99887741

In the above program I used an unnamed union, this way I dont have to access its members using a union object. An unnamed union is a union wich has not been defines as a type or a named object. blah.

char a; and int b; overlaps in memory. They both start at the same address in memory. This way together, they only take up as much space as the biggest one, wich is b.

If I write to a, b will change. However a will only change if I write to b's LO byte(first in memory, little endian).


Quote:
Then I would like to use it scroll through a list with those pointers, so I don't have to keep casting. Is this possible and is there a better way to do it?
I dont understand, do you have some code?
volatile is offline   Reply With Quote
Old Jun 9th, 2006, 7:17 AM   #3
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
A union is a type that has all its members occupying the same memory. So, in volatile's example &a == &b (if we ignore the types of a and b). The intent of a union is that it holds only one value at a time.
grumpy is offline   Reply With Quote
Old Jun 9th, 2006, 7:40 AM   #4
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
I've got what he wants -
In Windoze there is a variant datatype - which is represented in MS C by a big union.
This union allows you to reference a BSTR then use it as an int or a float.

Except: in vanilla C the void * does this for you. So you probably don't want a union.
jim mcnamara is offline   Reply With Quote
Old Jun 9th, 2006, 7:07 PM   #5
Harakim
Hobbyist Programmer
 
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3 Harakim is on a distinguished road
I am trying to help write a client application for a game server as I mentioned a while ago. Someone else sent me some of the code they were working on in the same project. Here is some of their code:
...
	switch(*msgType) {
		// server load
		case SV_LOAD:
			msgType+=6;
			break;
		// server tick
		case SV_TICK:
			msgType+=2;
			break;
		// setting the name of our own char (in 3 parts)
		case SV_SETCHAR_NAME1:
			memcpy(cplayer.name,msgType+1,15);
			msgType+=16;
			break;
		case SV_SETCHAR_NAME2:
			memcpy(cplayer.name+15,msgType+1,15);
			msgType+=16;
			break;
		case SV_SETCHAR_NAME3:
			memcpy(cplayer.name+30,msgType+1,10);
			msgType+=16;
			break;

		// setting mode (speed)
		case SV_SETCHAR_MODE:
			cplayer.mode = *(msgType+1);
			msgType+=2;
			break;

		// setting attributes (str, int, will, brav)
		case SV_SETCHAR_ATTRIB:
			cplayer.attrib[*(msgType+1)][0]=*(msgType+2);
			cplayer.attrib[*(msgType+1)][1]=*(msgType+3);
			cplayer.attrib[*(msgType+1)][2]=*(msgType+4);
			cplayer.attrib[*(msgType+1)][3]=*(msgType+5);
			cplayer.attrib[*(msgType+1)][4]=*(msgType+6);
			cplayer.attrib[*(msgType+1)][5]=*(msgType+7);
			msgType+=8;
			break;

		// setting skills
		case SV_SETCHAR_SKILL:
			cplayer.skill[*(msgType+1)][0]=*(msgType+2);
			cplayer.skill[*(msgType+1)][1]=*(msgType+3);
			cplayer.skill[*(msgType+1)][2]=*(msgType+4);
			cplayer.skill[*(msgType+1)][3]=*(msgType+5);
			cplayer.skill[*(msgType+1)][4]=*(msgType+6);
			cplayer.skill[*(msgType+1)][5]=*(msgType+7);
			msgType+=8;
			break;

		// setting hp
		case SV_SETCHAR_HP:
			cplayer.hp[0]=*(unsigned short*)(msgType+1);
			cplayer.hp[1]=*(unsigned short*)(msgType+3);
			cplayer.hp[2]=*(unsigned short*)(msgType+5);
			cplayer.hp[3]=*(unsigned short*)(msgType+7);
			cplayer.hp[4]=*(unsigned short*)(msgType+9);
			cplayer.hp[5]=*(unsigned short*)(msgType+11);
			msgType+=13;
			break;

		// setting endurance
		case SV_SETCHAR_ENDUR:
			cplayer.end[0]=*(unsigned short*)(msgType+1);
			cplayer.end[1]=*(unsigned short*)(msgType+3);
			cplayer.end[2]=*(unsigned short*)(msgType+5);
			cplayer.end[3]=*(unsigned short*)(msgType+7);
			cplayer.end[4]=*(unsigned short*)(msgType+9);
			cplayer.end[5]=*(unsigned short*)(msgType+11);
			msgType+=13;
			break;

		// setting mana
		case SV_SETCHAR_MANA:
			cplayer.mana[0]=*(unsigned short*)(msgType+1);
			cplayer.mana[1]=*(unsigned short*)(msgType+3);
			cplayer.mana[2]=*(unsigned short*)(msgType+5);
			cplayer.mana[3]=*(unsigned short*)(msgType+7);
			cplayer.mana[4]=*(unsigned short*)(msgType+9);
			cplayer.mana[5]=*(unsigned short*)(msgType+11);
			msgType+=13;
			break;

		// setting items
		case SV_SETCHAR_ITEM:
			cplayer.item[*(unsigned long*)(msgType+1)]=*(short int*)(msgType+5);
			cplayer.item_p[*(unsigned long*)(msgType+1)]=*(short int*)(msgType+7);
			msgType+=9;
			break;

		// setting equipped items
		case SV_SETCHAR_WORN:
			cplayer.worn[*(unsigned long*)(msgType+1)]=*(short int*)(msgType+5);
			cplayer.worn_p[*(unsigned long*)(msgType+1)]=*(short int*)(msgType+7);
			msgType+=9;
			break;
...
This is a portion of the "message handling" code of the client. The server sends a packet full of messages and this code will decode them 1 by 1. As you can see, this involves a lot of casting. I would like to use a union of pointers to remove the casting.

Thank you very much for your clear responses, you have made it clear what a union is and that it will be a good tool for my work. : )
Harakim is offline   Reply With Quote
Old Jun 13th, 2006, 1:41 PM   #6
Dragon_Master
Programmer
 
Dragon_Master's Avatar
 
Join Date: Jan 2006
Location: Some where
Posts: 74
Rep Power: 3 Dragon_Master is on a distinguished road
Honesetly, what's the point of unions.... What would be a practical use for them?
Dragon_Master is offline   Reply With Quote
Old Jun 16th, 2006, 11:16 AM   #7
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
They're a lot less useful now, but think back to when 1 Mb of memory was regarded as way too much. Space-saving measures were implemented any way they could be.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:33 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC