5 Replies - 620 Views - Last Post: 14 August 2010 - 06:24 PM Rate Topic: -----

#1 Eclipse Reborn  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 135
  • Joined: 26-June 10

[C++] Unions

Posted 14 August 2010 - 05:29 PM

I don't know if I understand them correctly, but from what I've read so far, they store everything in the same memory location.

Ex:
union test
{
	unsigned long	Count;
	unsigned long	Size;
	char*			Name;
	unsigned short	Len;
};

int main()
{
	test ut;
	
	ut.Count	= 20;
	ui.Size		= 15;
	
	ui.Name		= (char*)malloc(5);
	strcpy_s(ui.Name, 4, "test");
	
	ui.Len		= 15;
	
	free(ui.Name);
	return 0;
}


Now are there all stored in the same memory location?

Ex:
Count       Size        Name           Len
00 00 00 20 00 00 00 15 xx xx xx xx 00 00 15


Is This A Good Question/Topic? 1
  • +

Replies To: [C++] Unions

#2 OnlineCop  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 73
  • Joined: 16-April 10

Re: [C++] Unions

Posted 14 August 2010 - 05:52 PM

You're going to have a problem with that pointer in there. The pointer itself will take up 4 (or 8) bytes. But "Count", for example, will have a HUGE value (since the value will be something like 0XF3EA4F8A). And if you increment "Count" by 1, the pointer will no longer point to the first "t" in "test" but point to the "e" in "test". You could not use them interchangeably.

So for your last question, "Count" and "Size" actually start in the exact same place. If you set "Count" to 12, then "Size" will equal 12, the pointer to "Name" will be pointing to address 12 (or 0x0000000C), and "Len" would be 12. Not pretty.
Was This Post Helpful? 0
  • +
  • -

#3 Eclipse Reborn  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 135
  • Joined: 26-June 10

Re: [C++] Unions

Posted 14 August 2010 - 05:55 PM

Ah, ok think I'll pass on unions then.
Was This Post Helpful? 0
  • +
  • -

#4 woppix  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 112
  • Joined: 13-September 09

Re: [C++] Unions

Posted 14 August 2010 - 05:55 PM

from what i know about unions, they will allow you to access the same information different ways because each method of accessing that information points to the same location in memory.

This post has been edited by woppix: 14 August 2010 - 06:02 PM

Was This Post Helpful? 0
  • +
  • -

#5 Guest_c.user*


Reputation:

Re: [C++] Unions

Posted 14 August 2010 - 06:19 PM

    test ut;

    ut.Count    = 20;
    ui.Size     = 15;



sizeof ut is equal to size of the biggest element of the union
when you assign to ui.Size where is ui ? is it a mistake ?
I assume that was a mistake, so when you assign to ut.Size you are deleting the value of ut.Count because they take one place in memory

This post has been edited by c.user: 14 August 2010 - 06:20 PM

Was This Post Helpful? 0

#6 OnlineCop  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 73
  • Joined: 16-April 10

Re: [C++] Unions

Posted 14 August 2010 - 06:24 PM

One use of unions that I thought was well-done was an IP address converted to an integer. Another is to test whether the platform you were using was big-endian or little-endian:

union endian {
   char bytes[4];
   int integer;
};

int main()
{
   endian end;

   end.integer = 255;

   if (end.bytes[3] == 255) printf("Big Endian\n");
   else if (end.bytes[0] == 255) printf("Little Endian\n");
   else printf("Undetermined. Maybe 64-bit machine?\n")

   return 0;
}


Untested. I may have the big- and little-endian tests backward, but it will essentially fill bytes 0-3 with a value of either "0xFF000000" or "0x000000FF" depending on endianness. 64-bit machines will actually do the same type of thing, but use up 8 bytes instead of 4.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1