Why doesn't 'd /= d' throw a division by zero exception?Catching exception: divide by zeroc++ division by 0When to use virtual destructors?Why is “using namespace std;” considered bad practice?Why do we need virtual functions in C++?What is move semantics?C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?Why are elementwise additions much faster in separate loops than in a combined loop?Why is reading lines from stdin much slower in C++ than Python?Why is processing a sorted array faster than processing an unsorted array?Why should I use a pointer rather than the object itself?C++ code for testing the Collatz conjecture faster than hand-written assembly - why?

What is a CirKle Word™?

Tensorflow - logistic regrssion -oneHot Encoder - Transformed array of differt size for both train and test

Which note goes on which side of the stem?

Does travel insurance for short flight delays exist?

I don't have the theoretical background in my PhD topic. I can't justify getting the degree

Handling Disruptive Student on the Autistic Spectrum

Is gzip atomic?

What is the history of the university asylum law?

How to respectfully refuse to assist co-workers with IT issues?

Why doesn't 'd /= d' throw a division by zero exception?

What to say to a student who has failed?

Why were the crew so desperate to catch Truman and return him to Seahaven?

Is there any method of inflicting the incapacitated condition and no other condition?

Is there any way to keep a player from killing an NPC?

Is it possible to perform a regression where you have an unknown / unknowable feature variable?

How to gently end involvement with an online community?

Why did they avoid parodying Martian Manhunter?

Identify a problem where a potentially winning move draws because of the 50 move rule

LeetCode: Group Anagrams C#

How to find out the average duration of the peer-review process for a given journal?

Sci fi film similar to Village of the Damned

Architectural feasibility of a tiered circular stone keep

State-of-the-art algorithms for solving linear programming problems

Shouldn't the "credit score" prevent Americans from going deeper and deeper into personal debt?



Why doesn't 'd /= d' throw a division by zero exception?


Catching exception: divide by zeroc++ division by 0When to use virtual destructors?Why is “using namespace std;” considered bad practice?Why do we need virtual functions in C++?What is move semantics?C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?Why are elementwise additions much faster in separate loops than in a combined loop?Why is reading lines from stdin much slower in C++ than Python?Why is processing a sorted array faster than processing an unsorted array?Why should I use a pointer rather than the object itself?C++ code for testing the Collatz conjecture faster than hand-written assembly - why?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








7















I don't quite understand why I don't get a division by zero exception:



int d = 0;
d /= d;


I expected to get a division by zero exception but instead d == 1.










share|improve this question









New contributor



minebv is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1





    its a matter of definition what should be the result of 0/0. I don't know what the c++ standard says, but I am not aware of any case were you get an exception thrown without explicitly asking for

    – formerlyknownas_463035818
    8 hours ago






  • 2





    That’s undefined behavior.

    – L. F.
    8 hours ago






  • 5





    There's no such thing as a division by zero exception.

    – πάντα ῥεῖ
    8 hours ago











  • I'm loathe to close this as a duplicate - to my mind at least the interesting part is the output and the OP has taken care to state what that is.

    – Bathsheba
    8 hours ago






  • 2





    To clarify some of the comments: when you see a message about a "division by zero exception" that's the operating system telling you that something went wrong. It is not a C++ exception. In C++, exceptions are thrown by a throw statement. Nothing else (unless you're in undefined-behavior land).

    – Pete Becker
    7 hours ago


















7















I don't quite understand why I don't get a division by zero exception:



int d = 0;
d /= d;


I expected to get a division by zero exception but instead d == 1.










share|improve this question









New contributor



minebv is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1





    its a matter of definition what should be the result of 0/0. I don't know what the c++ standard says, but I am not aware of any case were you get an exception thrown without explicitly asking for

    – formerlyknownas_463035818
    8 hours ago






  • 2





    That’s undefined behavior.

    – L. F.
    8 hours ago






  • 5





    There's no such thing as a division by zero exception.

    – πάντα ῥεῖ
    8 hours ago











  • I'm loathe to close this as a duplicate - to my mind at least the interesting part is the output and the OP has taken care to state what that is.

    – Bathsheba
    8 hours ago






  • 2





    To clarify some of the comments: when you see a message about a "division by zero exception" that's the operating system telling you that something went wrong. It is not a C++ exception. In C++, exceptions are thrown by a throw statement. Nothing else (unless you're in undefined-behavior land).

    – Pete Becker
    7 hours ago














7












7








7


1






I don't quite understand why I don't get a division by zero exception:



int d = 0;
d /= d;


I expected to get a division by zero exception but instead d == 1.










share|improve this question









New contributor



minebv is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I don't quite understand why I don't get a division by zero exception:



int d = 0;
d /= d;


I expected to get a division by zero exception but instead d == 1.







c++






share|improve this question









New contributor



minebv is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










share|improve this question









New contributor



minebv is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|improve this question




share|improve this question








edited 4 hours ago









Boann

38.7k13 gold badges93 silver badges123 bronze badges




38.7k13 gold badges93 silver badges123 bronze badges






New contributor



minebv is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








asked 8 hours ago









minebvminebv

443 bronze badges




443 bronze badges




New contributor



minebv is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




minebv is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • 1





    its a matter of definition what should be the result of 0/0. I don't know what the c++ standard says, but I am not aware of any case were you get an exception thrown without explicitly asking for

    – formerlyknownas_463035818
    8 hours ago






  • 2





    That’s undefined behavior.

    – L. F.
    8 hours ago






  • 5





    There's no such thing as a division by zero exception.

    – πάντα ῥεῖ
    8 hours ago











  • I'm loathe to close this as a duplicate - to my mind at least the interesting part is the output and the OP has taken care to state what that is.

    – Bathsheba
    8 hours ago






  • 2





    To clarify some of the comments: when you see a message about a "division by zero exception" that's the operating system telling you that something went wrong. It is not a C++ exception. In C++, exceptions are thrown by a throw statement. Nothing else (unless you're in undefined-behavior land).

    – Pete Becker
    7 hours ago













  • 1





    its a matter of definition what should be the result of 0/0. I don't know what the c++ standard says, but I am not aware of any case were you get an exception thrown without explicitly asking for

    – formerlyknownas_463035818
    8 hours ago






  • 2





    That’s undefined behavior.

    – L. F.
    8 hours ago






  • 5





    There's no such thing as a division by zero exception.

    – πάντα ῥεῖ
    8 hours ago











  • I'm loathe to close this as a duplicate - to my mind at least the interesting part is the output and the OP has taken care to state what that is.

    – Bathsheba
    8 hours ago






  • 2





    To clarify some of the comments: when you see a message about a "division by zero exception" that's the operating system telling you that something went wrong. It is not a C++ exception. In C++, exceptions are thrown by a throw statement. Nothing else (unless you're in undefined-behavior land).

    – Pete Becker
    7 hours ago








1




1





its a matter of definition what should be the result of 0/0. I don't know what the c++ standard says, but I am not aware of any case were you get an exception thrown without explicitly asking for

– formerlyknownas_463035818
8 hours ago





its a matter of definition what should be the result of 0/0. I don't know what the c++ standard says, but I am not aware of any case were you get an exception thrown without explicitly asking for

– formerlyknownas_463035818
8 hours ago




2




2





That’s undefined behavior.

– L. F.
8 hours ago





That’s undefined behavior.

– L. F.
8 hours ago




5




5





There's no such thing as a division by zero exception.

– πάντα ῥεῖ
8 hours ago





There's no such thing as a division by zero exception.

– πάντα ῥεῖ
8 hours ago













I'm loathe to close this as a duplicate - to my mind at least the interesting part is the output and the OP has taken care to state what that is.

– Bathsheba
8 hours ago





I'm loathe to close this as a duplicate - to my mind at least the interesting part is the output and the OP has taken care to state what that is.

– Bathsheba
8 hours ago




2




2





To clarify some of the comments: when you see a message about a "division by zero exception" that's the operating system telling you that something went wrong. It is not a C++ exception. In C++, exceptions are thrown by a throw statement. Nothing else (unless you're in undefined-behavior land).

– Pete Becker
7 hours ago






To clarify some of the comments: when you see a message about a "division by zero exception" that's the operating system telling you that something went wrong. It is not a C++ exception. In C++, exceptions are thrown by a throw statement. Nothing else (unless you're in undefined-behavior land).

– Pete Becker
7 hours ago













2 Answers
2






active

oldest

votes


















9















C++ does not have a "Division by Zero" Exception to catch. The behavior you're observing is the result of Compiler optimizations:



  1. The compiler assumes Undefined Behavior doesn't happen

  2. Division by Zero in C++ is undefined behavior

  3. Therefore, code which can cause a Division by Zero is presumed to not do so.

    • And, code which must cause a Division by Zero is presumed to never happen


  4. Therefore, the compiler deduces that because Undefined Behavior doesn't happen, then the conditions for Undefined Behavior in this code (d == 0) must not happen

  5. Therefore, d / d must always equal 1.

However...



We can force the compiler to trigger a "real" division by zero with a minor tweak to your code.



volatile int d = 0;
d /= d; //What happens?


So now the question remains: now that we've basically forced the compiler to allow this to happen, what happens? It's undefined behavior—but we've now prevented the compiler from optimizing around this undefined behavior.



Mostly, it depends on the target environment. This will not trigger a software exception, but it can (depending on the target CPU) trigger a Hardware Exception (an Integer-Divide-by-Zero), which cannot be caught in the traditional manner a software exception can be caught. This is definitely the case for an x86 CPU, and most other (but not all!) architectures.



There are, however, methods of dealing with the hardware exception (if it occurs) instead of just letting the program crash: look at this post for some methods that might be applicable: Catching exception: divide by zero. Note they vary from compiler to compiler.






share|improve this answer



























  • With or without the 'volatile', a run with MSVC/VS-2019 gives: exited with code -1073741676!

    – Adrian
    7 hours ago











  • … switching to clang-cl, though, I get a 'random' value for d (nearly always 14684554) but no crash??

    – Adrian
    7 hours ago












  • @Adrian Both of those are perfectly ok since the behaviour is undefined. Literally anything is OK.

    – Jesper Juhl
    7 hours ago












  • "Division by Zero in C++ is undefined behavior" -> note that the compiler can't make this optimisation for floating point types under IEE754. It must set d to NaN.

    – Bathsheba
    7 hours ago











  • @Jesper Juhl Agree 100%. I'm just surprised, though.

    – Adrian
    7 hours ago


















8















The behaviour of integer division by zero is undefined by the C++ standard. It is not required to throw an exception.



(Floating point division by zero is also undefined but IEEE754 defines it.)



Your compiler is optimising d /= d to, effectively d = 1 which is a reasonable choice to make. It's allowed to make this optimisation since it's allowed to assume there is no undefined behaviour in your code - that is d cannot possibly be zero.






share|improve this answer






















  • 1





    It's important to be extra clear, that also something else could happen, IOW this behavior can't be relied on.

    – hyde
    8 hours ago











  • I think simply emphasizing "You compiler" would be enough.

    – François Andrieux
    7 hours ago











  • When you say it's reasonable for the compiler to assume "that is d cannot possibly be zero," do you also assume that the compiler doesn't see the line: int d = 0; ?? :)

    – Adrian
    7 hours ago











  • Compiler sees it, but probably doesn't care. The extra code complexity required in the already crazy complex compiler for an edge case like this probably isn't worth it.

    – user4581301
    7 hours ago











  • @user4581301 Taking both together allows it to detect a poisoned branch, allowing it to prune much more code. So it would be useful.

    – Deduplicator
    7 hours ago













Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






minebv is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57628986%2fwhy-doesnt-d-d-throw-a-division-by-zero-exception%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









9















C++ does not have a "Division by Zero" Exception to catch. The behavior you're observing is the result of Compiler optimizations:



  1. The compiler assumes Undefined Behavior doesn't happen

  2. Division by Zero in C++ is undefined behavior

  3. Therefore, code which can cause a Division by Zero is presumed to not do so.

    • And, code which must cause a Division by Zero is presumed to never happen


  4. Therefore, the compiler deduces that because Undefined Behavior doesn't happen, then the conditions for Undefined Behavior in this code (d == 0) must not happen

  5. Therefore, d / d must always equal 1.

However...



We can force the compiler to trigger a "real" division by zero with a minor tweak to your code.



volatile int d = 0;
d /= d; //What happens?


So now the question remains: now that we've basically forced the compiler to allow this to happen, what happens? It's undefined behavior—but we've now prevented the compiler from optimizing around this undefined behavior.



Mostly, it depends on the target environment. This will not trigger a software exception, but it can (depending on the target CPU) trigger a Hardware Exception (an Integer-Divide-by-Zero), which cannot be caught in the traditional manner a software exception can be caught. This is definitely the case for an x86 CPU, and most other (but not all!) architectures.



There are, however, methods of dealing with the hardware exception (if it occurs) instead of just letting the program crash: look at this post for some methods that might be applicable: Catching exception: divide by zero. Note they vary from compiler to compiler.






share|improve this answer



























  • With or without the 'volatile', a run with MSVC/VS-2019 gives: exited with code -1073741676!

    – Adrian
    7 hours ago











  • … switching to clang-cl, though, I get a 'random' value for d (nearly always 14684554) but no crash??

    – Adrian
    7 hours ago












  • @Adrian Both of those are perfectly ok since the behaviour is undefined. Literally anything is OK.

    – Jesper Juhl
    7 hours ago












  • "Division by Zero in C++ is undefined behavior" -> note that the compiler can't make this optimisation for floating point types under IEE754. It must set d to NaN.

    – Bathsheba
    7 hours ago











  • @Jesper Juhl Agree 100%. I'm just surprised, though.

    – Adrian
    7 hours ago















9















C++ does not have a "Division by Zero" Exception to catch. The behavior you're observing is the result of Compiler optimizations:



  1. The compiler assumes Undefined Behavior doesn't happen

  2. Division by Zero in C++ is undefined behavior

  3. Therefore, code which can cause a Division by Zero is presumed to not do so.

    • And, code which must cause a Division by Zero is presumed to never happen


  4. Therefore, the compiler deduces that because Undefined Behavior doesn't happen, then the conditions for Undefined Behavior in this code (d == 0) must not happen

  5. Therefore, d / d must always equal 1.

However...



We can force the compiler to trigger a "real" division by zero with a minor tweak to your code.



volatile int d = 0;
d /= d; //What happens?


So now the question remains: now that we've basically forced the compiler to allow this to happen, what happens? It's undefined behavior—but we've now prevented the compiler from optimizing around this undefined behavior.



Mostly, it depends on the target environment. This will not trigger a software exception, but it can (depending on the target CPU) trigger a Hardware Exception (an Integer-Divide-by-Zero), which cannot be caught in the traditional manner a software exception can be caught. This is definitely the case for an x86 CPU, and most other (but not all!) architectures.



There are, however, methods of dealing with the hardware exception (if it occurs) instead of just letting the program crash: look at this post for some methods that might be applicable: Catching exception: divide by zero. Note they vary from compiler to compiler.






share|improve this answer



























  • With or without the 'volatile', a run with MSVC/VS-2019 gives: exited with code -1073741676!

    – Adrian
    7 hours ago











  • … switching to clang-cl, though, I get a 'random' value for d (nearly always 14684554) but no crash??

    – Adrian
    7 hours ago












  • @Adrian Both of those are perfectly ok since the behaviour is undefined. Literally anything is OK.

    – Jesper Juhl
    7 hours ago












  • "Division by Zero in C++ is undefined behavior" -> note that the compiler can't make this optimisation for floating point types under IEE754. It must set d to NaN.

    – Bathsheba
    7 hours ago











  • @Jesper Juhl Agree 100%. I'm just surprised, though.

    – Adrian
    7 hours ago













9














9










9









C++ does not have a "Division by Zero" Exception to catch. The behavior you're observing is the result of Compiler optimizations:



  1. The compiler assumes Undefined Behavior doesn't happen

  2. Division by Zero in C++ is undefined behavior

  3. Therefore, code which can cause a Division by Zero is presumed to not do so.

    • And, code which must cause a Division by Zero is presumed to never happen


  4. Therefore, the compiler deduces that because Undefined Behavior doesn't happen, then the conditions for Undefined Behavior in this code (d == 0) must not happen

  5. Therefore, d / d must always equal 1.

However...



We can force the compiler to trigger a "real" division by zero with a minor tweak to your code.



volatile int d = 0;
d /= d; //What happens?


So now the question remains: now that we've basically forced the compiler to allow this to happen, what happens? It's undefined behavior—but we've now prevented the compiler from optimizing around this undefined behavior.



Mostly, it depends on the target environment. This will not trigger a software exception, but it can (depending on the target CPU) trigger a Hardware Exception (an Integer-Divide-by-Zero), which cannot be caught in the traditional manner a software exception can be caught. This is definitely the case for an x86 CPU, and most other (but not all!) architectures.



There are, however, methods of dealing with the hardware exception (if it occurs) instead of just letting the program crash: look at this post for some methods that might be applicable: Catching exception: divide by zero. Note they vary from compiler to compiler.






share|improve this answer















C++ does not have a "Division by Zero" Exception to catch. The behavior you're observing is the result of Compiler optimizations:



  1. The compiler assumes Undefined Behavior doesn't happen

  2. Division by Zero in C++ is undefined behavior

  3. Therefore, code which can cause a Division by Zero is presumed to not do so.

    • And, code which must cause a Division by Zero is presumed to never happen


  4. Therefore, the compiler deduces that because Undefined Behavior doesn't happen, then the conditions for Undefined Behavior in this code (d == 0) must not happen

  5. Therefore, d / d must always equal 1.

However...



We can force the compiler to trigger a "real" division by zero with a minor tweak to your code.



volatile int d = 0;
d /= d; //What happens?


So now the question remains: now that we've basically forced the compiler to allow this to happen, what happens? It's undefined behavior—but we've now prevented the compiler from optimizing around this undefined behavior.



Mostly, it depends on the target environment. This will not trigger a software exception, but it can (depending on the target CPU) trigger a Hardware Exception (an Integer-Divide-by-Zero), which cannot be caught in the traditional manner a software exception can be caught. This is definitely the case for an x86 CPU, and most other (but not all!) architectures.



There are, however, methods of dealing with the hardware exception (if it occurs) instead of just letting the program crash: look at this post for some methods that might be applicable: Catching exception: divide by zero. Note they vary from compiler to compiler.







share|improve this answer














share|improve this answer



share|improve this answer








edited 6 hours ago

























answered 7 hours ago









XiremaXirema

15.3k1 gold badge18 silver badges53 bronze badges




15.3k1 gold badge18 silver badges53 bronze badges















  • With or without the 'volatile', a run with MSVC/VS-2019 gives: exited with code -1073741676!

    – Adrian
    7 hours ago











  • … switching to clang-cl, though, I get a 'random' value for d (nearly always 14684554) but no crash??

    – Adrian
    7 hours ago












  • @Adrian Both of those are perfectly ok since the behaviour is undefined. Literally anything is OK.

    – Jesper Juhl
    7 hours ago












  • "Division by Zero in C++ is undefined behavior" -> note that the compiler can't make this optimisation for floating point types under IEE754. It must set d to NaN.

    – Bathsheba
    7 hours ago











  • @Jesper Juhl Agree 100%. I'm just surprised, though.

    – Adrian
    7 hours ago

















  • With or without the 'volatile', a run with MSVC/VS-2019 gives: exited with code -1073741676!

    – Adrian
    7 hours ago











  • … switching to clang-cl, though, I get a 'random' value for d (nearly always 14684554) but no crash??

    – Adrian
    7 hours ago












  • @Adrian Both of those are perfectly ok since the behaviour is undefined. Literally anything is OK.

    – Jesper Juhl
    7 hours ago












  • "Division by Zero in C++ is undefined behavior" -> note that the compiler can't make this optimisation for floating point types under IEE754. It must set d to NaN.

    – Bathsheba
    7 hours ago











  • @Jesper Juhl Agree 100%. I'm just surprised, though.

    – Adrian
    7 hours ago
















With or without the 'volatile', a run with MSVC/VS-2019 gives: exited with code -1073741676!

– Adrian
7 hours ago





With or without the 'volatile', a run with MSVC/VS-2019 gives: exited with code -1073741676!

– Adrian
7 hours ago













… switching to clang-cl, though, I get a 'random' value for d (nearly always 14684554) but no crash??

– Adrian
7 hours ago






… switching to clang-cl, though, I get a 'random' value for d (nearly always 14684554) but no crash??

– Adrian
7 hours ago














@Adrian Both of those are perfectly ok since the behaviour is undefined. Literally anything is OK.

– Jesper Juhl
7 hours ago






@Adrian Both of those are perfectly ok since the behaviour is undefined. Literally anything is OK.

– Jesper Juhl
7 hours ago














"Division by Zero in C++ is undefined behavior" -> note that the compiler can't make this optimisation for floating point types under IEE754. It must set d to NaN.

– Bathsheba
7 hours ago





"Division by Zero in C++ is undefined behavior" -> note that the compiler can't make this optimisation for floating point types under IEE754. It must set d to NaN.

– Bathsheba
7 hours ago













@Jesper Juhl Agree 100%. I'm just surprised, though.

– Adrian
7 hours ago





@Jesper Juhl Agree 100%. I'm just surprised, though.

– Adrian
7 hours ago













8















The behaviour of integer division by zero is undefined by the C++ standard. It is not required to throw an exception.



(Floating point division by zero is also undefined but IEEE754 defines it.)



Your compiler is optimising d /= d to, effectively d = 1 which is a reasonable choice to make. It's allowed to make this optimisation since it's allowed to assume there is no undefined behaviour in your code - that is d cannot possibly be zero.






share|improve this answer






















  • 1





    It's important to be extra clear, that also something else could happen, IOW this behavior can't be relied on.

    – hyde
    8 hours ago











  • I think simply emphasizing "You compiler" would be enough.

    – François Andrieux
    7 hours ago











  • When you say it's reasonable for the compiler to assume "that is d cannot possibly be zero," do you also assume that the compiler doesn't see the line: int d = 0; ?? :)

    – Adrian
    7 hours ago











  • Compiler sees it, but probably doesn't care. The extra code complexity required in the already crazy complex compiler for an edge case like this probably isn't worth it.

    – user4581301
    7 hours ago











  • @user4581301 Taking both together allows it to detect a poisoned branch, allowing it to prune much more code. So it would be useful.

    – Deduplicator
    7 hours ago















8















The behaviour of integer division by zero is undefined by the C++ standard. It is not required to throw an exception.



(Floating point division by zero is also undefined but IEEE754 defines it.)



Your compiler is optimising d /= d to, effectively d = 1 which is a reasonable choice to make. It's allowed to make this optimisation since it's allowed to assume there is no undefined behaviour in your code - that is d cannot possibly be zero.






share|improve this answer






















  • 1





    It's important to be extra clear, that also something else could happen, IOW this behavior can't be relied on.

    – hyde
    8 hours ago











  • I think simply emphasizing "You compiler" would be enough.

    – François Andrieux
    7 hours ago











  • When you say it's reasonable for the compiler to assume "that is d cannot possibly be zero," do you also assume that the compiler doesn't see the line: int d = 0; ?? :)

    – Adrian
    7 hours ago











  • Compiler sees it, but probably doesn't care. The extra code complexity required in the already crazy complex compiler for an edge case like this probably isn't worth it.

    – user4581301
    7 hours ago











  • @user4581301 Taking both together allows it to detect a poisoned branch, allowing it to prune much more code. So it would be useful.

    – Deduplicator
    7 hours ago













8














8










8









The behaviour of integer division by zero is undefined by the C++ standard. It is not required to throw an exception.



(Floating point division by zero is also undefined but IEEE754 defines it.)



Your compiler is optimising d /= d to, effectively d = 1 which is a reasonable choice to make. It's allowed to make this optimisation since it's allowed to assume there is no undefined behaviour in your code - that is d cannot possibly be zero.






share|improve this answer















The behaviour of integer division by zero is undefined by the C++ standard. It is not required to throw an exception.



(Floating point division by zero is also undefined but IEEE754 defines it.)



Your compiler is optimising d /= d to, effectively d = 1 which is a reasonable choice to make. It's allowed to make this optimisation since it's allowed to assume there is no undefined behaviour in your code - that is d cannot possibly be zero.







share|improve this answer














share|improve this answer



share|improve this answer








edited 7 hours ago

























answered 8 hours ago









BathshebaBathsheba

191k28 gold badges288 silver badges408 bronze badges




191k28 gold badges288 silver badges408 bronze badges










  • 1





    It's important to be extra clear, that also something else could happen, IOW this behavior can't be relied on.

    – hyde
    8 hours ago











  • I think simply emphasizing "You compiler" would be enough.

    – François Andrieux
    7 hours ago











  • When you say it's reasonable for the compiler to assume "that is d cannot possibly be zero," do you also assume that the compiler doesn't see the line: int d = 0; ?? :)

    – Adrian
    7 hours ago











  • Compiler sees it, but probably doesn't care. The extra code complexity required in the already crazy complex compiler for an edge case like this probably isn't worth it.

    – user4581301
    7 hours ago











  • @user4581301 Taking both together allows it to detect a poisoned branch, allowing it to prune much more code. So it would be useful.

    – Deduplicator
    7 hours ago












  • 1





    It's important to be extra clear, that also something else could happen, IOW this behavior can't be relied on.

    – hyde
    8 hours ago











  • I think simply emphasizing "You compiler" would be enough.

    – François Andrieux
    7 hours ago











  • When you say it's reasonable for the compiler to assume "that is d cannot possibly be zero," do you also assume that the compiler doesn't see the line: int d = 0; ?? :)

    – Adrian
    7 hours ago











  • Compiler sees it, but probably doesn't care. The extra code complexity required in the already crazy complex compiler for an edge case like this probably isn't worth it.

    – user4581301
    7 hours ago











  • @user4581301 Taking both together allows it to detect a poisoned branch, allowing it to prune much more code. So it would be useful.

    – Deduplicator
    7 hours ago







1




1





It's important to be extra clear, that also something else could happen, IOW this behavior can't be relied on.

– hyde
8 hours ago





It's important to be extra clear, that also something else could happen, IOW this behavior can't be relied on.

– hyde
8 hours ago













I think simply emphasizing "You compiler" would be enough.

– François Andrieux
7 hours ago





I think simply emphasizing "You compiler" would be enough.

– François Andrieux
7 hours ago













When you say it's reasonable for the compiler to assume "that is d cannot possibly be zero," do you also assume that the compiler doesn't see the line: int d = 0; ?? :)

– Adrian
7 hours ago





When you say it's reasonable for the compiler to assume "that is d cannot possibly be zero," do you also assume that the compiler doesn't see the line: int d = 0; ?? :)

– Adrian
7 hours ago













Compiler sees it, but probably doesn't care. The extra code complexity required in the already crazy complex compiler for an edge case like this probably isn't worth it.

– user4581301
7 hours ago





Compiler sees it, but probably doesn't care. The extra code complexity required in the already crazy complex compiler for an edge case like this probably isn't worth it.

– user4581301
7 hours ago













@user4581301 Taking both together allows it to detect a poisoned branch, allowing it to prune much more code. So it would be useful.

– Deduplicator
7 hours ago





@user4581301 Taking both together allows it to detect a poisoned branch, allowing it to prune much more code. So it would be useful.

– Deduplicator
7 hours ago










minebv is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















minebv is a new contributor. Be nice, and check out our Code of Conduct.












minebv is a new contributor. Be nice, and check out our Code of Conduct.











minebv is a new contributor. Be nice, and check out our Code of Conduct.














Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57628986%2fwhy-doesnt-d-d-throw-a-division-by-zero-exception%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

François Viète Contents Biography Work and thought Bibliography See also Notes Further reading External links Navigation menup. 21Google Bookspp. 75–77Google BooksDe thou (from University of Saint Andrews)ArchivedGoogle BooksGoogle BooksGoogle BooksGoogle booksGoogle Bookscc-parthenay.frL'histoire universelle (fr)Universal History (en)ArchivedAdsabs.harvard.eduPagesperso-orange.frArchive.orgChikara Sasaki. Descartes' mathematical thought p.259Google BooksGoogle BooksGoogle Bookspp. 152 and onwardGoogle BooksGoogle BooksScribd.comGoogle Books1257-7979Google BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGallica.bnf.frGoogle BooksGoogle Books"François Viète"Francois Viète: Father of Modern Algebraic NotationThe Lawyer and the GamblerAbout TarporleySite de Jean-Paul GuichardL'algèbre nouvelle"About the Harmonicon"cb120511976(data)1188044800000 0001 0913 5903n82164680ola2013766880073431702w6vt1sb70287374827140948071409480