Undefined Behaviour in C++ [duplicate] Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Program behaving strangely on online IDEsDetecting an undefined object propertyWhat are the differences between a pointer variable and a reference variable in C++?The Definitive C++ Book Guide and ListWhat is the “-->” operator in C++?How to determine if variable is 'undefined' or 'null'?How to check for “undefined” in JavaScript?What is the difference between null and undefined in JavaScript?Is there a standard function to check for null, undefined, or blank variables in JavaScript?Why is reading lines from stdin much slower in C++ than Python?Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviations
Random body shuffle every night—can we still function?
Customizing QGIS plugins
Dyck paths with extra diagonals from valleys (Laser construction)
How does Belgium enforce obligatory attendance in elections?
What does this say in Elvish?
Significance of Cersei's obsession with elephants?
Central Vacuuming: Is it worth it, and how does it compare to normal vacuuming?
Sliceness of knots
How do I tell what width chain my used chainring needs?
Lagrange four-squares theorem --- deterministic complexity
Induction Proof for Sequences
One-one communication
Why is it faster to reheat something than it is to cook it?
Should a wizard buy fine inks every time he want to copy spells into his spellbook?
How to compare two different files line by line in unix?
A letter with no particular backstory
Did any compiler fully use 80-bit floating point?
Why are vacuum tubes still used in amateur radios?
Antipodal Land Area Calculation
What is Adi Shankara referring to when he says "He has Vajra marks on his feet"?
Project Euler #1 in C++
How to write capital alpha?
C's equality operator on converted pointers
How does a spellshard spellbook work?
Undefined Behaviour in C++ [duplicate]
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Program behaving strangely on online IDEsDetecting an undefined object propertyWhat are the differences between a pointer variable and a reference variable in C++?The Definitive C++ Book Guide and ListWhat is the “-->” operator in C++?How to determine if variable is 'undefined' or 'null'?How to check for “undefined” in JavaScript?What is the difference between null and undefined in JavaScript?Is there a standard function to check for null, undefined, or blank variables in JavaScript?Why is reading lines from stdin much slower in C++ than Python?Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviations
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
This question already has an answer here:
Program behaving strangely on online IDEs
4 answers
I got this from a facebook post. What's happening here? See the output in ideone. Output is more than 10 lines.
Code:
#include<iostream>
using namespace std;
int main()
for (int i = 0; i < 10; ++i)
cout << i*1000000000 << endl;
Ideone Link
c++ undefined overflow
marked as duplicate by Peter
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 22 at 11:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Program behaving strangely on online IDEs
4 answers
I got this from a facebook post. What's happening here? See the output in ideone. Output is more than 10 lines.
Code:
#include<iostream>
using namespace std;
int main()
for (int i = 0; i < 10; ++i)
cout << i*1000000000 << endl;
Ideone Link
c++ undefined overflow
marked as duplicate by Peter
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 22 at 11:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
You even tagged it withoverflow.9*1000000000doesn't fit into anintanymore, which causes overflow, and that's undefined behavior.
– Blaze
Mar 22 at 10:55
2
Why can't you copy-paste the output into your question? And why do you think it's wrong or UB? Please read about how to ask good questions, as well as this question checklist.
– Some programmer dude
Mar 22 at 10:56
Probably compiler logic: "Since3 * 1000000000overflows, we can assume thati < 3. Thusi < 10is always true."
– molbdnilo
Mar 22 at 11:09
For a 16-bit int,3 * 1000000000will overflow wheni == 1. So the compiler can assumeiis always zero, and the conditioni < 10is always true. For a 32 bitint, the compiler can assumei < 3. For a 64-bitint, the loop will only run ten times.
– Peter
Mar 22 at 11:42
add a comment |
This question already has an answer here:
Program behaving strangely on online IDEs
4 answers
I got this from a facebook post. What's happening here? See the output in ideone. Output is more than 10 lines.
Code:
#include<iostream>
using namespace std;
int main()
for (int i = 0; i < 10; ++i)
cout << i*1000000000 << endl;
Ideone Link
c++ undefined overflow
This question already has an answer here:
Program behaving strangely on online IDEs
4 answers
I got this from a facebook post. What's happening here? See the output in ideone. Output is more than 10 lines.
Code:
#include<iostream>
using namespace std;
int main()
for (int i = 0; i < 10; ++i)
cout << i*1000000000 << endl;
Ideone Link
This question already has an answer here:
Program behaving strangely on online IDEs
4 answers
c++ undefined overflow
c++ undefined overflow
edited Mar 22 at 10:59
Inam
asked Mar 22 at 10:54
InamInam
83
83
marked as duplicate by Peter
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 22 at 11:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Peter
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 22 at 11:11
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
You even tagged it withoverflow.9*1000000000doesn't fit into anintanymore, which causes overflow, and that's undefined behavior.
– Blaze
Mar 22 at 10:55
2
Why can't you copy-paste the output into your question? And why do you think it's wrong or UB? Please read about how to ask good questions, as well as this question checklist.
– Some programmer dude
Mar 22 at 10:56
Probably compiler logic: "Since3 * 1000000000overflows, we can assume thati < 3. Thusi < 10is always true."
– molbdnilo
Mar 22 at 11:09
For a 16-bit int,3 * 1000000000will overflow wheni == 1. So the compiler can assumeiis always zero, and the conditioni < 10is always true. For a 32 bitint, the compiler can assumei < 3. For a 64-bitint, the loop will only run ten times.
– Peter
Mar 22 at 11:42
add a comment |
You even tagged it withoverflow.9*1000000000doesn't fit into anintanymore, which causes overflow, and that's undefined behavior.
– Blaze
Mar 22 at 10:55
2
Why can't you copy-paste the output into your question? And why do you think it's wrong or UB? Please read about how to ask good questions, as well as this question checklist.
– Some programmer dude
Mar 22 at 10:56
Probably compiler logic: "Since3 * 1000000000overflows, we can assume thati < 3. Thusi < 10is always true."
– molbdnilo
Mar 22 at 11:09
For a 16-bit int,3 * 1000000000will overflow wheni == 1. So the compiler can assumeiis always zero, and the conditioni < 10is always true. For a 32 bitint, the compiler can assumei < 3. For a 64-bitint, the loop will only run ten times.
– Peter
Mar 22 at 11:42
You even tagged it with
overflow. 9*1000000000 doesn't fit into an int anymore, which causes overflow, and that's undefined behavior.– Blaze
Mar 22 at 10:55
You even tagged it with
overflow. 9*1000000000 doesn't fit into an int anymore, which causes overflow, and that's undefined behavior.– Blaze
Mar 22 at 10:55
2
2
Why can't you copy-paste the output into your question? And why do you think it's wrong or UB? Please read about how to ask good questions, as well as this question checklist.
– Some programmer dude
Mar 22 at 10:56
Why can't you copy-paste the output into your question? And why do you think it's wrong or UB? Please read about how to ask good questions, as well as this question checklist.
– Some programmer dude
Mar 22 at 10:56
Probably compiler logic: "Since
3 * 1000000000 overflows, we can assume that i < 3. Thus i < 10 is always true."– molbdnilo
Mar 22 at 11:09
Probably compiler logic: "Since
3 * 1000000000 overflows, we can assume that i < 3. Thus i < 10 is always true."– molbdnilo
Mar 22 at 11:09
For a 16-bit int,
3 * 1000000000 will overflow when i == 1. So the compiler can assume i is always zero, and the condition i < 10 is always true. For a 32 bit int, the compiler can assume i < 3. For a 64-bit int, the loop will only run ten times.– Peter
Mar 22 at 11:42
For a 16-bit int,
3 * 1000000000 will overflow when i == 1. So the compiler can assume i is always zero, and the condition i < 10 is always true. For a 32 bit int, the compiler can assume i < 3. For a 64-bit int, the loop will only run ten times.– Peter
Mar 22 at 11:42
add a comment |
1 Answer
1
active
oldest
votes
Your platform most likely has a 32 bit int. So 1'000'000'000 is an int, and the compiler will attempt to evaluate i * 1'000'000'000 as an int too. This results in an overflow from i being 3 onwards.
The behaviour on overflowing a signed integral type is undefined.
Note that this makes the entire program behaviour undefined, which accounts for the multiple lines of output (beyond 10) that you observe.
(If you had chosen 10'000'000'000 say instead then the multiplication would have been evaluated with long long types and the behaviour would be well-defined!)
I know what UB is, but somehow it is always explainable what actually happens. Could you come up with a reason why this specific platform (ideone) causes it to run further than 10 ?
– Bart Friederichs
Mar 22 at 11:03
So due to the undefined behaviour somehow i < 10 condition is true when i >= 10?
– Inam
Mar 22 at 11:05
@Inam: My wildest guess is that the compiler is assuming thati * 1000000000can fit into anint, so thereforeimust be small, and therefore less than 10.
– Bathsheba
Mar 22 at 11:13
@Inam the point is that due to undefined behaviour; the whole program breaks down. The language that describes what the program should do is invalid. You've written a loop there, but the compiler may or may not decide to make it a loop anyway; but the compiler might detect that it only needs to do 3 loops before the value would overflow... and then bin the other 7
– UKMonkey
Mar 22 at 11:16
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your platform most likely has a 32 bit int. So 1'000'000'000 is an int, and the compiler will attempt to evaluate i * 1'000'000'000 as an int too. This results in an overflow from i being 3 onwards.
The behaviour on overflowing a signed integral type is undefined.
Note that this makes the entire program behaviour undefined, which accounts for the multiple lines of output (beyond 10) that you observe.
(If you had chosen 10'000'000'000 say instead then the multiplication would have been evaluated with long long types and the behaviour would be well-defined!)
I know what UB is, but somehow it is always explainable what actually happens. Could you come up with a reason why this specific platform (ideone) causes it to run further than 10 ?
– Bart Friederichs
Mar 22 at 11:03
So due to the undefined behaviour somehow i < 10 condition is true when i >= 10?
– Inam
Mar 22 at 11:05
@Inam: My wildest guess is that the compiler is assuming thati * 1000000000can fit into anint, so thereforeimust be small, and therefore less than 10.
– Bathsheba
Mar 22 at 11:13
@Inam the point is that due to undefined behaviour; the whole program breaks down. The language that describes what the program should do is invalid. You've written a loop there, but the compiler may or may not decide to make it a loop anyway; but the compiler might detect that it only needs to do 3 loops before the value would overflow... and then bin the other 7
– UKMonkey
Mar 22 at 11:16
add a comment |
Your platform most likely has a 32 bit int. So 1'000'000'000 is an int, and the compiler will attempt to evaluate i * 1'000'000'000 as an int too. This results in an overflow from i being 3 onwards.
The behaviour on overflowing a signed integral type is undefined.
Note that this makes the entire program behaviour undefined, which accounts for the multiple lines of output (beyond 10) that you observe.
(If you had chosen 10'000'000'000 say instead then the multiplication would have been evaluated with long long types and the behaviour would be well-defined!)
I know what UB is, but somehow it is always explainable what actually happens. Could you come up with a reason why this specific platform (ideone) causes it to run further than 10 ?
– Bart Friederichs
Mar 22 at 11:03
So due to the undefined behaviour somehow i < 10 condition is true when i >= 10?
– Inam
Mar 22 at 11:05
@Inam: My wildest guess is that the compiler is assuming thati * 1000000000can fit into anint, so thereforeimust be small, and therefore less than 10.
– Bathsheba
Mar 22 at 11:13
@Inam the point is that due to undefined behaviour; the whole program breaks down. The language that describes what the program should do is invalid. You've written a loop there, but the compiler may or may not decide to make it a loop anyway; but the compiler might detect that it only needs to do 3 loops before the value would overflow... and then bin the other 7
– UKMonkey
Mar 22 at 11:16
add a comment |
Your platform most likely has a 32 bit int. So 1'000'000'000 is an int, and the compiler will attempt to evaluate i * 1'000'000'000 as an int too. This results in an overflow from i being 3 onwards.
The behaviour on overflowing a signed integral type is undefined.
Note that this makes the entire program behaviour undefined, which accounts for the multiple lines of output (beyond 10) that you observe.
(If you had chosen 10'000'000'000 say instead then the multiplication would have been evaluated with long long types and the behaviour would be well-defined!)
Your platform most likely has a 32 bit int. So 1'000'000'000 is an int, and the compiler will attempt to evaluate i * 1'000'000'000 as an int too. This results in an overflow from i being 3 onwards.
The behaviour on overflowing a signed integral type is undefined.
Note that this makes the entire program behaviour undefined, which accounts for the multiple lines of output (beyond 10) that you observe.
(If you had chosen 10'000'000'000 say instead then the multiplication would have been evaluated with long long types and the behaviour would be well-defined!)
edited Mar 22 at 11:16
answered Mar 22 at 10:56
BathshebaBathsheba
182k27257385
182k27257385
I know what UB is, but somehow it is always explainable what actually happens. Could you come up with a reason why this specific platform (ideone) causes it to run further than 10 ?
– Bart Friederichs
Mar 22 at 11:03
So due to the undefined behaviour somehow i < 10 condition is true when i >= 10?
– Inam
Mar 22 at 11:05
@Inam: My wildest guess is that the compiler is assuming thati * 1000000000can fit into anint, so thereforeimust be small, and therefore less than 10.
– Bathsheba
Mar 22 at 11:13
@Inam the point is that due to undefined behaviour; the whole program breaks down. The language that describes what the program should do is invalid. You've written a loop there, but the compiler may or may not decide to make it a loop anyway; but the compiler might detect that it only needs to do 3 loops before the value would overflow... and then bin the other 7
– UKMonkey
Mar 22 at 11:16
add a comment |
I know what UB is, but somehow it is always explainable what actually happens. Could you come up with a reason why this specific platform (ideone) causes it to run further than 10 ?
– Bart Friederichs
Mar 22 at 11:03
So due to the undefined behaviour somehow i < 10 condition is true when i >= 10?
– Inam
Mar 22 at 11:05
@Inam: My wildest guess is that the compiler is assuming thati * 1000000000can fit into anint, so thereforeimust be small, and therefore less than 10.
– Bathsheba
Mar 22 at 11:13
@Inam the point is that due to undefined behaviour; the whole program breaks down. The language that describes what the program should do is invalid. You've written a loop there, but the compiler may or may not decide to make it a loop anyway; but the compiler might detect that it only needs to do 3 loops before the value would overflow... and then bin the other 7
– UKMonkey
Mar 22 at 11:16
I know what UB is, but somehow it is always explainable what actually happens. Could you come up with a reason why this specific platform (ideone) causes it to run further than 10 ?
– Bart Friederichs
Mar 22 at 11:03
I know what UB is, but somehow it is always explainable what actually happens. Could you come up with a reason why this specific platform (ideone) causes it to run further than 10 ?
– Bart Friederichs
Mar 22 at 11:03
So due to the undefined behaviour somehow i < 10 condition is true when i >= 10?
– Inam
Mar 22 at 11:05
So due to the undefined behaviour somehow i < 10 condition is true when i >= 10?
– Inam
Mar 22 at 11:05
@Inam: My wildest guess is that the compiler is assuming that
i * 1000000000 can fit into an int, so therefore i must be small, and therefore less than 10.– Bathsheba
Mar 22 at 11:13
@Inam: My wildest guess is that the compiler is assuming that
i * 1000000000 can fit into an int, so therefore i must be small, and therefore less than 10.– Bathsheba
Mar 22 at 11:13
@Inam the point is that due to undefined behaviour; the whole program breaks down. The language that describes what the program should do is invalid. You've written a loop there, but the compiler may or may not decide to make it a loop anyway; but the compiler might detect that it only needs to do 3 loops before the value would overflow... and then bin the other 7
– UKMonkey
Mar 22 at 11:16
@Inam the point is that due to undefined behaviour; the whole program breaks down. The language that describes what the program should do is invalid. You've written a loop there, but the compiler may or may not decide to make it a loop anyway; but the compiler might detect that it only needs to do 3 loops before the value would overflow... and then bin the other 7
– UKMonkey
Mar 22 at 11:16
add a comment |
You even tagged it with
overflow.9*1000000000doesn't fit into anintanymore, which causes overflow, and that's undefined behavior.– Blaze
Mar 22 at 10:55
2
Why can't you copy-paste the output into your question? And why do you think it's wrong or UB? Please read about how to ask good questions, as well as this question checklist.
– Some programmer dude
Mar 22 at 10:56
Probably compiler logic: "Since
3 * 1000000000overflows, we can assume thati < 3. Thusi < 10is always true."– molbdnilo
Mar 22 at 11:09
For a 16-bit int,
3 * 1000000000will overflow wheni == 1. So the compiler can assumeiis always zero, and the conditioni < 10is always true. For a 32 bitint, the compiler can assumei < 3. For a 64-bitint, the loop will only run ten times.– Peter
Mar 22 at 11:42