473,796 Members | 2,603 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Code crashes at runtime - is this valid c++

Compile with:
g++ crash.cpp -O2

Crashes on:
g++ (Gentoo 4.3.2 p1.1) 4.3.2
g++ (GCC) 4.3.0 20080428 (Red Hat 4.3.0-8)
g++ (ubuntu current stable)

Steps to reproduce:
1. Make a file crash.cpp (contents below)
2. g++ crash.cpp -O2
3. ./a.out
Floating point exception

Source:

#include <iostream>
#include <fenv.h>

float g(const float& x)
{
float y = x;
if(y == 0.0f)
{
y=1.0f;
}
return x/y;
}

void f(const float &z)
{
if (z 0.0f)
{
float Unit2 = g(z);

std::cout << Unit2;
}
}

int main()
{
feenableexcept( FE_INVALID);

float a = 130.0f;
f(a);

return 0;
}
Nov 6 '08 #1
15 1683
ch************* @gmail.com wrote:
Compile with:
g++ crash.cpp -O2

Crashes on:
g++ (Gentoo 4.3.2 p1.1) 4.3.2
g++ (GCC) 4.3.0 20080428 (Red Hat 4.3.0-8)
g++ (ubuntu current stable)
Doesn't crash here with:
g++ (Ubuntu 4.3.2-1ubuntu11) 4.3.2

Steps to reproduce:
1. Make a file crash.cpp (contents below)
2. g++ crash.cpp -O2
3. ./a.out
Floating point exception
It prints '1' here, which is what I would have expected.
Source:

#include <iostream>
#include <fenv.h>

float g(const float& x)
{
float y = x;
if(y == 0.0f)
{
y=1.0f;
}
return x/y;
}

void f(const float &z)
{
if (z 0.0f)
{
float Unit2 = g(z);

std::cout << Unit2;
}
}

int main()
{
feenableexcept( FE_INVALID);

float a = 130.0f;
f(a);

return 0;
}
Nov 6 '08 #2
On Nov 6, 2:59 pm, chrisstankev... @gmail.com wrote:
Compile with:
g++ crash.cpp -O2

Crashes on:
g++ (Gentoo 4.3.2 p1.1) 4.3.2
g++ (GCC) 4.3.0 20080428 (Red Hat 4.3.0-8)
g++ (ubuntu current stable)

Steps to reproduce:
1. Make a file crash.cpp (contents below)
2. g++ crash.cpp -O2
3. ./a.out
Floating point exception

Source:

#include <iostream>
#include <fenv.h>

float g(const float& x)
{
float y = x;
if(y == 0.0f)
Never compare 2 floats for equality.
Apart from the fact that its an intensive operation for a cpu,
the following is expected...

#include <iostream>

int main()
{
float fa(0.0f);
float fb(0.0f);

if( fa == fb)
std::cout << "not equal\n";
else
std::cout << "equal\n";

std::cout << "Press ENTER to EXIT.\n";
std::cin.get();
}

/*
not equal
*/
{
y=1.0f;
}
return x/y;

}

void f(const float &z)
{
if (z 0.0f)
z might be 0.0000000001f
{
float Unit2 = g(z);

std::cout << Unit2;
}

}

int main()
{
feenableexcept( FE_INVALID);

float a = 130.0f;
f(a);

return 0;

}
Nov 6 '08 #3
On 2008-11-06 17:22:06 -0500, Salt_Peter <pj*****@yahoo. comsaid:
On Nov 6, 2:59 pm, chrisstankev... @gmail.com wrote:
>>
float g(const float& x)
{
float y = x;
if(y == 0.0f)

Never compare 2 floats for equality.
Seems like a pretty good way to avoid dividing by zero.
Apart from the fact that its an intensive operation for a cpu,
Um, no. It's nearly trivial.
the following is expected...

#include <iostream>

int main()
{
float fa(0.0f);
float fb(0.0f);

if( fa == fb)
std::cout << "not equal\n";
else
std::cout << "equal\n";

std::cout << "Press ENTER to EXIT.\n";
std::cin.get();
}

/*
not equal
*/
A standarde-conforming C++ compiler will not produce that result. Both
values are exact, and the comparison is exact.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Nov 6 '08 #4
On Nov 6, 12:38*pm, Rolf Magnus <ramag...@t-online.dewrote:
It prints '1' here, which is what I would have expected.
Peter,

Thanks that's good that it works for you. Did you use the -O2
option? On three distributions it does not work for me: gentoo,
fedora, and ubuntu.

Chris
Nov 6 '08 #5
On Nov 6, 2:22*pm, Salt_Peter <pj_h...@yahoo. comwrote:
Never compare 2 floats for equality.
Salt,

1. I agree.

2. Does my code crash with your compiler when compiled with -O2?

Thanks,

Chris
Nov 6 '08 #6

Crashes on:
g++ (Gentoo 4.3.2 p1.1) 4.3.2
g++ (GCC) 4.3.0 20080428 (Red Hat 4.3.0-8)
g++ (Ubuntu 4.3.2-1ubuntu11) 4.3.2 (my machine, but maybe not Rolf's)

Does not crash on:
g++-4.1.2 (GCC) 4.1.2 (Gentoo 4.1.2 p1.1)

I'm trying SVN version of g++ now.

Chris

PS: Rolf, sorry for calling you Peter earlier.
Nov 6 '08 #7
Salt_Peter wrote:
>...
float g(const float& x)
{
float y = x;
if(y == 0.0f)

Never compare 2 floats for equality.
This is a popular _fake_ rule. There are contexts where it is perfectly
valid to compare machine floats for equality. You just need to
understand what you are doing.
Apart from the fact that its an intensive operation for a cpu,
the following is expected...

#include <iostream>

int main()
{
float fa(0.0f);
float fb(0.0f);

if( fa == fb)
std::cout << "not equal\n";
else
std::cout << "equal\n";

std::cout << "Press ENTER to EXIT.\n";
std::cin.get();
}

/*
not equal
*/

No, this is not expected. Quite the opposite, the implementation that
produces this result is clearly broken.
--
Best regards,
Andrey Tarasevich
Nov 6 '08 #8
On Nov 6, 11:22 pm, Salt_Peter <pj_h...@yahoo. comwrote:
On Nov 6, 2:59 pm, chrisstankev... @gmail.com wrote:
Compile with:
g++ crash.cpp -O2
Crashes on:
g++ (Gentoo 4.3.2 p1.1) 4.3.2
g++ (GCC) 4.3.0 20080428 (Red Hat 4.3.0-8)
g++ (ubuntu current stable)
I can't reproduce it on any of the systems I have available
here. (Solaris on Sparc, with g++ 4.1.0 or Sun CC 5.8; Linux
2.6.9 on Intel, with g++ 4.1.0, and Windows XP with VC++ 14).
Of course, for all but Linux, I had to modify the code to get it
to compile.
Steps to reproduce:
1. Make a file crash.cpp (contents below)
2. g++ crash.cpp -O2
3. ./a.out
Floating point exception
Source:
#include <iostream>
#include <fenv.h>
This header is not standard C++, and is only available if you
have C99 as well. (Which means that I would normally expect it
pretty much everywhere---but my expectations are disappointed;
it's present neither under Solaris nor Windows.)
float g(const float& x)
{
float y = x;
if(y == 0.0f)
Never compare 2 floats for equality.
Which is very poor advice. In this particular case, comparison
for equality is exactly what he wants.
Apart from the fact that its an intensive operation for a cpu,
Since when? It's as fast as any other comparison, integral or
floating point, on the machines available to me.
the following is expected...
#include <iostream>
int main()
{
float fa(0.0f);
float fb(0.0f);
if( fa == fb)
std::cout << "not equal\n";
else
std::cout << "equal\n";
std::cout << "Press ENTER to EXIT.\n";
std::cin.get();
}
/*
not equal
*/
Are you kidding? Any compiler which outputs "not equal" with
that program is seriously broken.
{
y=1.0f;
}
return x/y;
}
void f(const float &z)
{
if (z 0.0f)
z might be 0.0000000001f
And z might be -0.000000001f. Or 1e30. Or anything else. The
whole point is that he doesn't know, and that he doesn't want to
divide by zero. The only correct test is thus:

if ( z != 0.0F )

Anything else is a programming error, and shows a complete lack
of understanding of how machine floating point works.

Note that in his actual code, the function is only called with z
equal 130.0. Which can't be 0.0, anyway you cut it, and will
always be greater than 0.0. (For that matter, I don't know of a
machine where it won't be represented exactly.)
{
float Unit2 = g(z);
std::cout << Unit2;
}
}
int main()
{
feenableexcept( FE_INVALID);
float a = 130.0f;
f(a);
return 0;
}
For the original poster: does adding the option -ffloat-store
have any effect? G++ isn't fully standards compliant without
it. Not that I think that it could affect your program; all of
the values you use are exactly representable. (It could affect
the program if the value you are comparing with 0.0 is the
result of an expression, and in some strange way, the compiler
picks it up from a register in the comparison, but reloads it
from memory for the division. And the actual real intermediate
results were smaller than 1E-38.) For Intel processors, g++
also accepts -mpc64 and -mpc32, which cause all intermediate
values to be rounded to 64 or 32 bits---this will not only
eliminate excess precision in the intermediate values, but may
also run a little bit faster (particularly on older hardware.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 7 '08 #9
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Rolf Magnus wrote:
ch************* @gmail.com wrote:
>Compile with:
g++ crash.cpp -O2

Crashes on:
g++ (Gentoo 4.3.2 p1.1) 4.3.2
g++ (GCC) 4.3.0 20080428 (Red Hat 4.3.0-8)
g++ (ubuntu current stable)

Doesn't crash here with:
g++ (Ubuntu 4.3.2-1ubuntu11) 4.3.2
On gcc version 4.3.0 20080428 (Red Hat 4.3.0-8) got from Fedora 9
repositories also works correctly.

Pawel Dziepak
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iEYEARECAAYFAkk TV2UACgkQPFW+cU iIHNqDKwCfcaNU4 o20I8NJ7qAhH/6ayGfH
73EAoKnn9MmwvG5 4pHzEwf0mFY1j9N Ru
=r1+s
-----END PGP SIGNATURE-----
Nov 7 '08 #10

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

Similar topics

12
2493
by: Paul | last post by:
Hi, Global operator new and delete are overloaded and I am using stl map to store pointers, but this code crashes, can some one shed some light??? Compiler: MS VC++ 6.0 STL: Shipped with Visual Studio. #include <malloc.h> #include <map>
0
1663
by: Mike Treadway | last post by:
Here's a posting from someone else that describes my problem. The issue is happening on two Windows 2000 machines that have Veritas installed. That's the only obvious common thing I can see between the two machines. Someone Else: I am developing a three tiered Windows Service application using remoting. The Server portion of the program runs on a Windows Server 2k3 machine and intermittently crashes. The event log contains the...
40
3058
by: Neo The One | last post by:
I think C# is forcing us to write more code by enforcing a rule that can be summarized as 'A local variable must be assgined *explicitly* before reading its value.' If you are interested in what I mean, please look at this feedback my me: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=3074c204-04e4-4383-9dd2-d266472a84ac If you think I am right, please vote for this feedback.
2
2016
by: Harry Simpson | last post by:
I've got a call within my .NET code to a function which uses an API call to get printer settings. When there's an error in the API call code, it crashes the .NET app. Try Catch doesn't prevent the crash - it's like it crashes before the catch catches..... Any ideas how to error handle this type of function so it doesn't crash the application?? TIA
0
1652
by: Norman Diamond | last post by:
When Visual Studio 2005 SP1 crashes, sometimes it offers to send a dump to Microsoft, but sometimes it doesn't. Is there some way to encourage it to send dumps more often? One crash resulted in this event in the application event log: $B%$%Y%s%H$N<oN`(B: $B%(%i!<(B $B%$%Y%s%H(B $B%=!<%9(B: .NET Runtime $B%$%Y%s%H(B $B%+%F%4%j(B: $B$J$7(B $B%$%Y%s%H(B ID: 1023 $BF|IU(B: 2007/07/24
2
2164
by: Chris Paxton | last post by:
I have a fairly basic VB.NET application that I converted from VS2003 to VS2005 and now it crashed the VS2005 IDE pretty regularly while trying to use the form designer. I've removed any 3rd references in my projects and recoded methods to remove any deprecated functions. Anyone have any suggestions?
7
9110
by: whutto | last post by:
I have a VB.NET app that crashes when run from the Windows scheduled tasks, but runs fine when the run-as user is logged on. The app also runs fine from the scheduled jobs if the run as user is disconnected. The O/S is Windows Server 2003, the app is created with VB.NET 2003. The .NET framework is up todate with Windows releases. The APP opens connections to an SQL Server 2003 database and an ODBC sql connection to an iSeries database. ...
0
1748
by: Jameson | last post by:
Visual Studio 2008 (90 day trial, team suite edition) crashes with "Microsoft Visual Studio 2008 has encountered a problem and needs to close...." message when I try to create a new wpf application project. Also, loading an existing wpf project (the "hittest3d" demo) works, but the program terminates when the system tries to display the test project main window (ie. step into the program, debugging just stops at new window1()). Possibly...
1
1482
by: minima producciones | last post by:
Hello, The scenario is this: VB.net 2003 connecting to Access 2003 using Jet 4.0 SP8 and MDAC 2.7 SP1. On a particular client machine with XP SP2, while trying to fill a dataset it crashes fantastically without ANY error message. the exe just disappears from the task manager. I use a Module to launch the MainApp form, if I try to connect within
0
9685
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
9535
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10242
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
10200
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
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7558
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
6800
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4127
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.