public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/58417] New: Incorrect optimization
@ 2013-09-13 20:21 mirzayanovmr at gmail dot com
  2013-09-13 21:40 ` [Bug tree-optimization/58417] " glisse at gcc dot gnu.org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: mirzayanovmr at gmail dot com @ 2013-09-13 20:21 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58417

            Bug ID: 58417
           Summary: Incorrect optimization
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mirzayanovmr at gmail dot com

Created attachment 30820
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=30820&action=edit
Compile it with "g++ -O2 a.cpp" and run. It will output 0 instead of 10.

The following code writes 0 (but 10 is expected) with -O1 or -O2. Checked on
4.7.2 and 4.8.1 (mingw), on Fedora with g++ (GCC) 4.8.1 20130603 (Red Hat
4.8.1-1).

Command line to compile: g++ -O2 a.cpp

Code:

#include<iostream>

using namespace std;

long long arr[6] = {0, 1, 2, 3, 4, 5};

int main()
{
    int n = 5;
    long long sum = 0, prevsum = 0;

    for(int i = 1; i <= n; i++)
    {
        cout << "sum = " << sum << endl;
        sum = (i - 1) * arr[i] - prevsum;
        // cout<<"sum : "<<sum<<endl;
        prevsum += arr[i];
    }

    cout << "Final Result: " << sum << endl;
}


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2013-09-20 17:50 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-09-13 20:21 [Bug c++/58417] New: Incorrect optimization mirzayanovmr at gmail dot com
2013-09-13 21:40 ` [Bug tree-optimization/58417] " glisse at gcc dot gnu.org
2013-09-16  8:38 ` [Bug tree-optimization/58417] Incorrect optimization in SCEV const-prop rguenth at gcc dot gnu.org
2013-09-16  8:40 ` rguenth at gcc dot gnu.org
2013-09-16 11:28 ` rguenth at gcc dot gnu.org
2013-09-16 11:59 ` rguenth at gcc dot gnu.org
2013-09-17  9:51 ` rguenth at gcc dot gnu.org
2013-09-18 12:31 ` rguenth at gcc dot gnu.org
2013-09-18 12:32 ` rguenth at gcc dot gnu.org
2013-09-20 10:19 ` rguenth at gcc dot gnu.org
2013-09-20 17:50 ` rguenth 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).