public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/96538] New: Integer overflow when there are multiple operands in addition operation.
@ 2020-08-08 19:52 prateek_kd at yahoo dot co.in
2020-08-08 20:15 ` [Bug c++/96538] " prateek_kd at yahoo dot co.in
2020-08-08 22:51 ` pinskia at gcc dot gnu.org
0 siblings, 2 replies; 3+ messages in thread
From: prateek_kd at yahoo dot co.in @ 2020-08-08 19:52 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96538
Bug ID: 96538
Summary: Integer overflow when there are multiple operands in
addition operation.
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: prateek_kd at yahoo dot co.in
Target Milestone: ---
I am trying to add int values present in the vector and store in long int type
variable. If I am using for loop for calculating the sum of vector elements
program produces correct sum. However if If I perform direct addition(e.g sum=
arr[0]+arr[1]...=arr[n]), the results obtained is incorrect and seems that
overflow is occurring. Below is the code snippet for you reference:
Input:942381765 627450398 954173620 583762094 236817490
Code leading to overflow/error:
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<int> arr(5);
for(int i=0;i<5;i++)
{
cin>>arr[i];
}
sort(arr.begin(),arr.end());
long int sum = 0,min_sum=0,max_sum=0;
sum = arr[1]+arr[2]+arr[3]
min_sum = arr[0]+ sum;
max_sum = arr[4]+ sum;
cout<<min_sum<<" "<<max_sum<<endl;
return 0;
}
Output :-1904555549 -1187199419
Code producing correct output:
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<int> arr(5);
for(int i=0;i<5;i++)
{
cin>>arr[i];
}
sort(arr.begin(),arr.end());
long int sum = 0,min_sum=0,max_sum=0;
for(int j=0;j<5;j++)
{
sum = sum+ arr[j];//after for loop sum = arr[1]+arr[2]+arr[3]
}
min_sum = arr[0]+ sum;
max_sum = arr[4]+ sum;
cout<<min_sum<<" "<<max_sum<<endl;
return 0;
}
Output(for same input as above):
3581402857 4298758987
^ permalink raw reply [flat|nested] 3+ messages in thread
* [Bug c++/96538] Integer overflow when there are multiple operands in addition operation.
2020-08-08 19:52 [Bug c++/96538] New: Integer overflow when there are multiple operands in addition operation prateek_kd at yahoo dot co.in
@ 2020-08-08 20:15 ` prateek_kd at yahoo dot co.in
2020-08-08 22:51 ` pinskia at gcc dot gnu.org
1 sibling, 0 replies; 3+ messages in thread
From: prateek_kd at yahoo dot co.in @ 2020-08-08 20:15 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96538
--- Comment #1 from Prateek Khade <prateek_kd at yahoo dot co.in> ---
(In reply to Prateek Khade from comment #0)
> I am trying to add int values present in the vector and store in long int
> type variable. If I am using for loop for calculating the sum of vector
> elements program produces correct sum. However if If I perform direct
> addition(e.g sum= arr[0]+arr[1]...=arr[n]), the results obtained is
> incorrect and seems that overflow is occurring. Below is the code snippet
> for you reference:
>
> Input:942381765 627450398 954173620 583762094 236817490
>
>
> Code leading to overflow/error:
> #include<bits/stdc++.h>
> using namespace std;
>
> int main()
> {
> vector<int> arr(5);
> for(int i=0;i<5;i++)
> {
> cin>>arr[i];
> }
> sort(arr.begin(),arr.end());
> long int sum = 0,min_sum=0,max_sum=0;
> sum = arr[1]+arr[2]+arr[3]
> min_sum = arr[0]+ sum;
> max_sum = arr[4]+ sum;
> cout<<min_sum<<" "<<max_sum<<endl;
>
> return 0;
> }
>
>
> Output :-1904555549 -1187199419
>
> Code producing correct output:
>
> #include<bits/stdc++.h>
> using namespace std;
>
> int main()
> {
> vector<int> arr(5);
> for(int i=0;i<5;i++)
> {
> cin>>arr[i];
> }
> sort(arr.begin(),arr.end());
> long int sum = 0,min_sum=0,max_sum=0;
> for(int j=1;j<4;j++)
> {
> sum = sum+ arr[j];//after for loop sum = arr[1]+arr[2]+arr[3]
> }
> min_sum = arr[0]+ sum;
> max_sum = arr[4]+ sum;
> cout<<min_sum<<" "<<max_sum<<endl;
>
> return 0;
> }
>
>
>
> Output(for same input as above):
> 3581402857 4298758987
^ permalink raw reply [flat|nested] 3+ messages in thread
* [Bug c++/96538] Integer overflow when there are multiple operands in addition operation.
2020-08-08 19:52 [Bug c++/96538] New: Integer overflow when there are multiple operands in addition operation prateek_kd at yahoo dot co.in
2020-08-08 20:15 ` [Bug c++/96538] " prateek_kd at yahoo dot co.in
@ 2020-08-08 22:51 ` pinskia at gcc dot gnu.org
1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2020-08-08 22:51 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96538
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Resolution|--- |INVALID
--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
There is NO BUG here:
sum = arr[1]+arr[2]+arr[3];
Is done in integer type and then casted to long.
that is it is the same as:
sum = (long)(arr[1]+arr[2]+arr[3]);
While:
for(int j=0;j<5;j++)
{
sum = sum+ arr[j];
}
Is Done in long type.
You can fix the original code by doing:
sum = ((long)arr[1])+((long)arr[2])+((long)arr[3]);
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2020-08-08 22:51 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-08 19:52 [Bug c++/96538] New: Integer overflow when there are multiple operands in addition operation prateek_kd at yahoo dot co.in
2020-08-08 20:15 ` [Bug c++/96538] " prateek_kd at yahoo dot co.in
2020-08-08 22:51 ` pinskia at gcc dot gnu.org
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).