Solved

Issue in WIN-8 64-bit (Divide by zero exception)

Posted on 2014-09-30
11
221 Views
Last Modified: 2014-10-25
1. Application is made using Embarcadero Builder XE - 6.
2. Application setting changed to locale DE(German) and found working fine in all OSs(WIN XP, WIN-7, WIN-8 64-bit)
3. If Application setting changed to locale English(United State), it works fine in XP,WIN7 but fails in WIN-8 64-bit.(FYI: WIN-8 machine is installed as English).
4. Issue is divide by zero exception.
5. Divide by zero gives -NAN, where the s/w is working fine. But why throwing exception(Abnormal program termination) in WIN-8 64-bit only in English while German version is working fine(I kept log in place of exception and found it's converting to -NAN).
6. I want to know why the behavior is changing from one locale to another?
0
Comment
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 28

Expert Comment

by:pepr
ID: 40353977
There is a simple answer that will not help you. There is a bug in the application.

It is difficult to tell the reason without debugging the application or without having the source code. Say, one of the versions works with lengths of the language related text. Say, the author of other language support forgot to translate some text and the length of the missing (translated) text is zero by default. Say, something is bigger or smaller in the other language and some parts of memory is not initialized or is overwritten by mistake. Say, the other locale uses a different format of anything converted to a text, and spoils some calculation somehow. There can be tons of reasons.
0
 
LVL 8

Author Comment

by:Subrat (C++ windows/Linux)
ID: 40354066
0
 
LVL 28

Assisted Solution

by:pepr
pepr earned 150 total points
ID: 40354097
I see. I think the CodeProject discussion is going to find the reason. I do not have the compiler and it seems to be related to the compiler details.

It would be nice--when the problem is solved--if you summarized shortly the observation/solution here, and then you can accept your own comment. You will not get nor loose your points.
0
 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 40354363
as per the C++ standard there's no exception defined for divide by zero. As far as I know under such conditions behaviour is undefined.
So I wouldn't be surprised with the behaviour you are seeing.

I'm quoting Stroustrup here in "The Design and Evolution of C++" (Addison Wesley, 1994) :


"low-level events, such as arithmetic overflows and divide by zero, are assumed to be handled by a dedicated lower-level mechanism rather than by exceptions. This enables C++ to match the behaviour of other languages when it comes to arithmetic. It also avoids the problems that occur on heavily pipelined architectures where events such as divide by zero are asynchronous."


Also Section 5.6 of the C++ standard also mentions this

If the second operand of / or % is zero the behavior is undefined
0
 
LVL 32

Expert Comment

by:sarabande
ID: 40354364
a likely reason for the issue is that the locale changed to numeric separators, hence period and comma characters would exchange their meaning for numbers in string format what might lead to a conversion failure.

to test whether that is the reason you may change the locale to german and check. you also could check for string to number conversions in the code and whether the result of such a conversion was used as divisor or in a modulo operation.

if it fails with the german locale as well, it still could be a conversion issue as the os is English. you may try with a German win-64 then.

Sara
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 32

Expert Comment

by:sarabande
ID: 40354396
as per the C++ standard there's no exception defined for divide by zero.
win32 exceptions contain
- c0000094 integer division by zero
- c000008e Floating-point division by zero

Sara
0
 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 40354561
win32 exceptions contain
- c0000094 integer division by zero
- c000008e Floating-point division by zero

these are win32 exceptions. I didn't know that the C++ standard was Windows specific

what's defined in the standard is this

class logic_error;
class domain_error;
class invalid_argument;
class length_error;
class out_of_range;
class runtime_error;
class range_error;
class overflow_error;
class underflow_error;


Do we see a divide-by-zero exception here? Or perhaps Stroustrup is wrong in what he said...

a likely reason for the issue is that the locale changed to numeric separators, hence period and comma characters would exchange their meaning for numbers in string format
 you also could check for string to number conversions in the code and whether the result of such a conversion was used as divisor or in a modulo operation.

The OP has not mentioned any string conversions.
0
 
LVL 32

Assisted Solution

by:sarabande
sarabande earned 175 total points
ID: 40354663
these are win32 exceptions. I didn't know that the C++ standard was Windows specific
win api is an ansi c library. when using windows api, win32 exceptions may be thrown regardless whether the c++ standard defined them or not.
see http://msdn.microsoft.com/en-us/library/windows/desktop/ms679329(v=vs.85).aspx

The OP has not mentioned any string conversions
when a division by zero error happens after changing the locale to "German" or "Swiss", it is a good chance that it happened because of the different numerical (string) formats. see the comment of pepr who also made a similar assumption.

when the crash could reproduced in the debugger, the wrong statement should be seen in the call stack.

I kept log in place of exception and found it's converting to -NAN
does that mean that you already found the faulty statement? if yes, is the divisor -NAN or the result of the division?

Sara
0
 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 40356211
win api is an ansi c library. when using windows api, win32 exceptions may be thrown regardless whether the c++ standard defined them or not.

The point is that the C++ standard doesn't require an exception to be generated when a div by zero is attempted. Behavior is undefined by the standard.

The root cause may be due to string formats or various other reasons but for a Div by Zero the C++ standard doesn't define behavior and that's something Subrat ALSO needs to know.
0
 
LVL 32

Expert Comment

by:sarabande
ID: 40356439
the win api was made long before c++ standard and so I fail to see the point. c++ allows to use standard c libraries and the exceptions thrown are not c++ exceptions but SEH exceptions. though both are called 'exceptions' a c++ try-catch would not be able to catch them beside proprietary compiler options were set to enable it. the error here was reported by the operation system in the event viewer and not by the application what is an indication that the code either didn't try to catch the exception first-case or was not enabled to catch SEH exceptions.

anyhow, there is no reason to have doubts that the exceptions were thrown and that a divide-by-zero case happened after changing the locale.

Sara
0
 
LVL 12

Accepted Solution

by:
trinitrotoluene earned 175 total points
ID: 40358610
though both are called 'exceptions' a c++ try-catch would not be able to catch them
Technically speaking a div by zero is not an exception. Its an error detected at the hardware level and then communicated up to the application via the OS.
Windows provides SEH as a way to handle it and that makes it platform specific.
A c++ try catch wouldn't catch it, I didn't say otherwise. Check my earlier post where I've quoted Stroustrup.

The point I'm trying to make is that if the OP is writing C++ code then there should be an awareness as to what is supported by the C++ standard and what is left to the OS.
All the more relevant since this is a C++ forum and not a Windows one.

there is no reason to have doubts that the exceptions were thrown
exactly my point. Windows might choose to throw up 0xC0000094: Integer division by zero or something else.
Another platform need not necessarily use the same mechanism to inform the user level.

The best way forward is to isolate code to the Maximum extent POSSIBLE which could cause the error and then check for a division by zero.
Thereby you as the programmer are not relying on the OS to inform you. Eventually write platform agnostic C++ code and to do that you need to be aware of what the C++ standard supports.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now