Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 272
  • Last Modified:

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

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
Subrat (C++ windows/Linux)
Asked:
Subrat (C++ windows/Linux)
  • 4
  • 4
  • 2
  • +1
3 Solutions
 
peprCommented:
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
 
Subrat (C++ windows/Linux)Software EngineerAuthor Commented:
0
 
peprCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
trinitrotolueneCommented:
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
 
sarabandeCommented:
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
 
sarabandeCommented:
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
 
trinitrotolueneCommented:
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
 
sarabandeCommented:
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
 
trinitrotolueneCommented:
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
 
sarabandeCommented:
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
 
trinitrotolueneCommented:
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 4
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now