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;








0
















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










share|improve this question















marked as duplicate by Peter c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

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 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





    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












  • 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

















0
















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










share|improve this question















marked as duplicate by Peter c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

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 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





    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












  • 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













0












0








0









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










share|improve this question

















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 10:59







Inam

















asked Mar 22 at 10:54









InamInam

83




83




marked as duplicate by Peter c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

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 c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

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 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





    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












  • 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

















  • 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





    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












  • 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
















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












1 Answer
1






active

oldest

votes


















3














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!)






share|improve this answer

























  • 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 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

















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














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!)






share|improve this answer

























  • 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 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















3














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!)






share|improve this answer

























  • 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 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













3












3








3







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!)






share|improve this answer















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!)







share|improve this answer














share|improve this answer



share|improve this answer








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 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

















  • 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 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
















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





Popular posts from this blog

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해