LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-09-2010, 10:38 AM   #1
rajitsingh
LQ Newbie
 
Registered: Mar 2010
Posts: 10

Rep: Reputation: 0
C (++) Unions trouble


Just playin around, i created this union:

Code:
union name1{
	int i;
	float f;
	double d;
};
int main(){
	name1 n1;
	n1.f=2.32;
	cout<<"n1.f= "<<n1.i<<endl;   \\case 1, returns garbage
	cout<<"n1.f= "<<n1.f<<endl;   \\case 2
	cout<<"n1.f= "<<n1.d<<endl;   \\case 3, returns garbage
	return 0;
}
Now, if all the members of a union share the same memory space, then why do 'case 1' and 'case 3' return garbage values?
I thought it would be so with 'case 1' because of narrowing, but what about 'case 3'??
 
Old 05-09-2010, 11:03 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
because you assigned garbage in. (From the perspective of an integer)
 
Old 05-09-2010, 11:53 AM   #3
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Quote:
Originally Posted by rajitsingh View Post
Code:
union name1{
	int i;
	float f;
	double d;
};
int main(){
	name1 n1;
	n1.f=2.32;
	cout<<"n1.f= "<<n1.i<<endl;   \\case 1, returns garbage
	cout<<"n1.f= "<<n1.f<<endl;   \\case 2
	cout<<"n1.f= "<<n1.d<<endl;   \\case 3, returns garbage
	return 0;
}
It depends on what you consider as garbage :-) To give you a bit of more context than graemef although he's totally correct!

Because you might not now all the details of how floating point numbers are represented in binary mode (IEEE specification), I'll try to explain this using plain integer types. Assume you have a union with two members, having integer (32-bit) and short (16-bit) types, whereby both obviously overlap in memory.

The 16-bit short member covers the low/high (depending on the endianess) 16-bit of the integer type. Though, say you set the integer to 0x00001234 on a little-endian system, then the short will have the value of 0x1234. On a big-endian system however, the short value will be 0x0000.

Though, the memory region pointed to by a union is interpreted according to the type by which this memory region is accessed.

I hope that somehow helps to understand why your app produces garbage for the cases 1 and 3.

- Andi -
 
Old 05-09-2010, 05:47 PM   #4
ArthurSittler
Member
 
Registered: Jul 2008
Distribution: Slackware
Posts: 124

Rep: Reputation: 30
please don't violate type in unions

Unions are not provided as a method to circumvent the type protections of the C language. You are trying to use unions in a way that is prohibited by the semantics of the language, though the compiler will not and actually can not prohibit the violation.

Using unions is actually deprecated in C++. C++ adopts the syntax, grammar, and semantics of C. That is why it was named C++ and not something completely different, such as "Objectiva". C++ grudgingly provides unions to support the principle that a good C program is an acceptable C++ program. There are well written C programs that employ unions, but those programs do not abuse unions as you were attempting to do. C++ provides inheritance and polymorphism to provide equivalent functionality.

You should not be surprised that you can not read back a union in a type different from the type that was last written to the union. The documentation of the C programming language states explicitly that it is the programmer's responsibility to keep track of what type was last written to the union and only read back the type last written to the union. A union may be initialized when it is defined only as the first member type.

You should not attempt to use a union to manipulate or convert the union member to a different type. There are casts and library functions for that purpose. The format of data in unions is explicitly implementation dependent in the C language. This means that even if you can glean information about the format of one data type by looking it as a different data type, that information may not be portable to any other compiler or any other machine architecture. In fact, it may not be portable even to a later or earlier version of the same compiler on the same machine architecture.

If you would like to examine the format of different types of data, you may be able to do so in a debugger which displays the binary representation of the data in hex format. Don't count on the format of that type of data to be the same in any other context.
 
Old 05-10-2010, 05:37 AM   #5
rajitsingh
LQ Newbie
 
Registered: Mar 2010
Posts: 10

Original Poster
Rep: Reputation: 0
Are you all trying to say that it could be because different types are represented using different representations in memory, and the garbage is produced when i try to read a given representation using a different interpretation?
 
Old 05-10-2010, 06:59 AM   #6
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,076

Rep: Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110
Quote:
Originally Posted by rajitsingh View Post
Are you all trying to say that it could be because different types are represented using different representations in memory, and the garbage is produced when i try to read a given representation using a different interpretation?
Change "could be" to "is" and you have your answer.

I do not mean that any two different data types necessarily have different representations. In some architectures, int and long have the same representation. It is less common, but in some architectures float and double have the same representation (as each other, not same as int or long).

I mean that when an operation on a union, such as the one you did, produces garbage that is because the representation is different.

Last edited by johnsfine; 05-10-2010 at 03:16 PM.
 
Old 05-10-2010, 07:14 AM   #7
ArthurSittler
Member
 
Registered: Jul 2008
Distribution: Slackware
Posts: 124

Rep: Reputation: 30
Different type data has different representation in memory

Yes, that is the case. You description is much better and more concise than my somewhat rambling explanation.

The representation we use for things that we simply count (integers) does not apply as aptly to quantities that we measure (floats). Though we could estimate the number of water molecules in a glass of water, we could not in fact count them. We assign an integer identification number to each record in a database because there can not be an arbitrarily small part of a record. And neither integer nor float format would be a very good method for storing the text of this message.

All information stored by computers is represented as binary numbers of possibly different numbers of bits, depending on the type of object we want to represent. Even in objects represented by the same number of bits, the meaning of the bits differ when they represent different types of objects.
 
Old 05-10-2010, 10:16 AM   #8
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
You might want to take a look at this wikipedia page on floating point.Specifically look at the section on general layout. Then have a gander at this page on Signed number representations.

So yes the internal representation depends upon the data type. Different operating systems, computer architecture, programming language (take your pick) may also vary the internal representation. I hope these links help.
 
Old 05-10-2010, 02:52 PM   #9
rajitsingh
LQ Newbie
 
Registered: Mar 2010
Posts: 10

Original Poster
Rep: Reputation: 0
Thanks everyone, I get it now.
 
  


Reply

Tags
c++, programming, trouble, union


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C Unions MTK358 Programming 4 03-18-2010 08:57 AM
unions spx2 Programming 3 08-21-2006 09:35 AM
array of unions in c alaios Programming 3 09-19-2005 09:02 AM
array of unions (c code) alaios Programming 6 09-16-2005 11:43 AM
structres and unions problem linux_lover2005 Programming 2 04-15-2005 08:31 AM


All times are GMT -5. The time now is 09:41 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration