473,756 Members | 8,110 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overflow Underflow


union SignedChoice{
long with_sign;
unsigned long without_sign;
};
int main()
{
SignedChoice data;

data.with_sign = -1;
//Right now, does the C++ Standard guarantee that
//data.without_si gn == MAX_ULONGINT?

//Similarly:

data.without_si gn = MAX_ULONGINT;

//Is data.with_sign now definitely == -1?

}
Is there any other interesting facts yous can give me?

-JKop
Jul 22 '05
38 8454
Rob Williscroft wrote:
Yes, what I mean I said just after the bit you quoted, here it is
again:

is still UB, but in practice its only UB because the standard
says so.

Yes I meant, "where does the standard say so"?

Section 9 Classes, 9.5 Unions, paragraph 1.

I read that and I did not find something that would cause undefined
behaviour, in the contrary it says:

"The size of a union is sufficient to contain the largest of its data
members. Each data member is allocated as if it were the sole member
of a struct."
which makes the behaviour well-defined for signed and unsigned integer
types in a union with a value in
[0, maximum_positiv e_value_support ed_by_the_small er_range_type]


Regards,

Ioannis Vranos
Jul 22 '05 #31
Ioannis Vranos wrote:
Rob Williscroft wrote:
Yes, what I mean I said just after the bit you quoted, here it is
again:

is still UB, but in practice its only UB because the standard says so.

Yes I meant, "where does the standard say so"?
Section 9 Classes, 9.5 Unions, paragraph 1.


I read that and I did not find something that would cause undefined
behaviour, in the contrary it says:

"The size of a union is sufficient to contain the largest of its data
members. Each data member is allocated as if it were the sole member
of a struct."
which makes the behaviour well-defined for signed and unsigned integer
types


of the same size,
in a union with a value in
[0, maximum_positiv e_value_support ed_by_the_small er_range_type]



Regards,

Ioannis Vranos
Jul 22 '05 #32
Ioannis Vranos wrote:
I read that and I did not find something that would cause undefined
behaviour, in the contrary it says:

"The size of a union is sufficient to contain the largest of its data
members. Each data member is allocated as if it were the sole member
of a struct."
which makes the behaviour well-defined for signed and unsigned integer
types

of the same size,

of similar types (e.g. signed int with unsigned int)
in a union with a value in
[0, maximum_positiv e_value_support ed_by_the_small er_range_type]



Regards,

Ioannis Vranos
Jul 22 '05 #33
Ioannis Vranos wrote in news:cb******** ***@ulysses.noc .ntua.gr in
comp.lang.c++:
Rob Williscroft wrote:
Yes, what I mean I said just after the bit you quoted, here it is
again:

is still UB, but in practice its only UB because the standard
says so.
Yes I meant, "where does the standard say so"?
Section 9 Classes, 9.5 Unions, paragraph 1.

I read that and I did not find something that would cause undefined
behaviour, in the contrary it says:


The section (9.5) defines *all* the behaviour, that which it doesen't
define is *undefined*.
"The size of a union is sufficient to contain the largest of its data
members. Each data member is allocated as if it were the sole member
of a struct."

Irrelevant, you may only inspect the member that was most reciently
assigned too, *nothing* else is *defined* by the standard.

which makes the behaviour well-defined for signed and unsigned integer
types in a union with a value in
[0, maximum_positiv e_value_support ed_by_the_small er_range_type]


Again:

.... In theory (for example) a compiler could diagnose the
UB and refuse to compile, its UB, its free to *anything* it wants
and still be a conforming compiler.

Also an implementation could have "type" bits in the object
representation of its signed and unsigned types, simply
reading the wrong union member could cause the CPU to fault.

If it *isn't* defined its undefined, whats so difficult
to understand about this.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #34
Rob Williscroft wrote:
which makes the behaviour well-defined for signed and unsigned integer
types in a union with a value in
[0, maximum_positiv e_value_support ed_by_the_small er_range_type]

Again:

.... In theory (for example) a compiler could diagnose the
UB and refuse to compile, its UB, its free to *anything* it wants
and still be a conforming compiler.

Also an implementation could have "type" bits in the object
representation of its signed and unsigned types, simply
reading the wrong union member could cause the CPU to fault.

If it *isn't* defined its undefined, whats so difficult
to understand about this.

The only thing that makes you right and this undefined is the wording of
the standard:

"The size of a union is sufficient to contain the largest of its data
members."

The "sufficient " makes it undefined. If it were "equal to the largest of
its data members", it would not be undefined in any way.
So you are right, it is undefined, because in theory it can be larger
than the largest member and place them in a slightly different location.


Regards,

Ioannis Vranos
Jul 22 '05 #35
On Wed, 30 Jun 2004 02:16:07 +0300, Ioannis Vranos
<iv*@guesswh.at .grad.com> wrote:
Rob Williscroft wrote:
Yes, what I mean I said just after the bit you quoted, here it is
again:

is still UB, but in practice its only UB because the standard
says so.
Yes I meant, "where does the standard say so"?

Section 9 Classes, 9.5 Unions, paragraph 1.

I read that and I did not find something that would cause undefined
behaviour, in the contrary it says:

"The size of a union is sufficient to contain the largest of its data
members. Each data member is allocated as if it were the sole member
of a struct."
which makes the behaviour well-defined for signed and unsigned integer
types in a union with a value in
[0, maximum_positiv e_value_support ed_by_the_small er_range_type]


Did you not read the very first sentence of 9.5/1? Seems pretty
clear...

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #36
tom_usenet wrote:
Did you not read the very first sentence of 9.5/1? Seems pretty
clear...

Yes you are right, I just noticed it.


Regards,

Ioannis Vranos
Jul 22 '05 #37
JKop <NU**@NULL.NULL > wrote:
Old Wolf posted:
It's undefined behaviour to read any member of a union other than
the one that was most recently assigned (with the exception of the
case where the member read, and the most recently assigned member,
are 'struct's with a common initial sequence, and you read one of
the members of that common initial sequence).
But... is it Undefined Behaviour to read from an uninitialized int?


Yes. It's UB to access an indeterminate value, and uninitialized
variables all have indeterminate value.
If *not*, then contrary to you last post, there's nothing wrong with the
following:

union Chase
{
char black[sizeof(long)];
unsigned long blue;
};
pursuit.black[0] = 5;
pursuit.black[1] = 2;
pursuit.black[2] = 6;
pursuit.black[3] = 88;
pursuit.blue += 4;


This is nothing to do with reading from uninitialized variables.
Perhaps you missed what I said above:
It's undefined behaviour to read any member of a union other than
the one that was most recently assigned


Your example is a union, and you assign 'black' and then read 'blue'.
Your code is still UB even if you assign something to blue before
you assign to black.

If you want an exact reference in the standard, do a text search
for 'union' on your pirate copy
Jul 22 '05 #38
Old Wolf posted:
If you want an exact reference in the standard, do a text search
for 'union' on your pirate copy

I don't have a copy, hence the requests.
-JKop
Jul 22 '05 #39

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
8497
by: Oplec | last post by:
Hello, How can underflow and overflow of the basic arithmetic types be tested for using standard C++? For instance, if two long doubles are multiplied together, test whether or not the result is too large to fit in a long double? Thank you for your time, Oplec.
4
3554
by: aling | last post by:
Given: signed a, b; How to judge overflow of the sum of these two operands? Just use one *if* statement. Is this statement right? if ((a>0 && b>0 && sum<=0) || (a<0 && b<0 && sum>=0)) // overflow else // not overflow
2
6072
by: Paul Emmons | last post by:
Can anyone suggest example code, or how to proceed, in adding or subtracting two long long integers (I'm working in gcc with Linux, where a long long is 64 bits) and determining whether an overflow or underflow occurs, invalidating the result? I have written a solution in assembler. It's simple, thanks to the overflow flag. But without access to this part of the hardware in the C language, how would one do it? Thank you for your...
5
9232
by: Ian Pilcher | last post by:
I'm trying to figure out if an increment to a variable of an integer type, followed by a decrement, (or vice versa) is guaranteed to restore the variable to its initial value, even if the first operation causes the variable to overflow/underflow. In other words, if foo_t is an integer type, are the following two functions guaranteed to *always* return a non-zero value? int check_overflow(foo_t f) {
4
3604
by: glenn | last post by:
I have a COM Server I've written in C#. I have a client app I've written in Delphi that is calling the C# COM Server. However, one of the functions in the COM Server creates a form and during the creation process it receives the Overflow or Underflow in arithmetic operation error. I have traced it down to the section of the for where its registering the components... this.Controls.Add(this.cmd_exit); it doesn't matter which control...
4
4147
by: Tom | last post by:
I have a VB.NET framework 1.1 application that I am installing on my user's workstation. It works fine on EVERY machine except for one - on this one machine it generates a 'Overflow or underflow in the arithmetic operation' when I attempt to instantiate my first form, as so: Dim frm As New frmMyForm() Based on some things I saw here, I thought it might be that this workstation didn't have a MS Sans Seriff font on it (since that is the...
4
9951
by: Raymond | last post by:
Source: http://moryton.blogspot.com/2007/08/detecting-overflowunderflow-when.html Example from source: char unsigned augend (255); char unsigned const addend (255); char unsigned const sum (augend + addend); if (sum < augend)
2
3088
by: jou00jou | last post by:
Hi, I have trouble using sscanf and fgets to check for overflow. I will post the assignment specification so I could help whoever would kindly like to offer his/her help. ____________________________________________________________________________________ 1) The program should expect 2 decimal integers per line. Let's call these n1 and n2. 2) For each such input line it should output the value of n1 + 2 * n2 (followed by a newline...
11
3199
by: pereges | last post by:
Hi, I have written a small function that can (I think) deal with overflow/underflow while adding integers and divide by zero problem. Can some one please tell me if I have done it correctly ? Would it be safe to extend it to double precision floating point numbers since we know that there is always some margin for error while adding floating point numbers ? What other functions should I look to implement for my project ? #include...
0
9456
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9873
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.