public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/34459]  New: incorrect code when compiled with optimization (-O1)
@ 2007-12-14  6:29 noblige at gmail dot com
  2007-12-14 13:32 ` [Bug c++/34459] [4.1/4.3 Regression] " rguenth at gcc dot gnu dot org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: noblige at gmail dot com @ 2007-12-14  6:29 UTC (permalink / raw)
  To: gcc-bugs

The G++ produces invalid code for snippet bellow when compiled with -O1.

Using built-in specs.
Target: i486-linux
Configured with: ../src/configure -v --enable-languages=c,c++ --prefix=/usr
--enable-shared --with-system-zlib --libexecdir=/usr/lib
--without-included-gettext --enable-threads=posix --enable-nls
--program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu
--enable-libstdcxx-debug --enable-checking=release i486-linux
Thread model: posix
gcc version 4.1.2 20060613 (prerelease) (Debian 4.1.1-5)

Program output:
a=content: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
f=content: 14 90 90 1 30 0 74 9 10 0 80 9 10 0 1 0 0 0 c0 2d e 90 f0 2 30

Expected behavior would be to have both variables initialized to zeroes - this
is OK in non-optimized build (-O0).

Here's the source snippet:

#include <iostream>
#include <sstream>

using std::string;

struct ip_addr {
    string to_string() const {
        std::stringstream os;
        os << "content: ";
        for (size_t i = 0; i < sizeof(addr_); ++i) {
            os << std::hex
                << (short(addr_[i]) & 0xff) << " ";
        }
        return os.str();
    }        
    ip_addr() {
        memset(&addr_, 0, sizeof(addr_));
    }
private:
    char addr_[25];
};

struct foo {
    foo(ip_addr const & a) : remote_(a) {
        rebind(remote_);
    }
    void rebind(ip_addr const & to) {
        remote_ = to;
    }
    ip_addr remote_;
};
int main()
{
    ip_addr a;
    std::cout << "a=" << a.to_string() << std::endl;
    foo f(a);
    std::cout << "f=" << f.remote_.to_string() << std::endl;
}


-- 
           Summary: incorrect code when compiled with optimization (-O1)
           Product: gcc
           Version: 4.1.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: noblige at gmail dot com
 GCC build triplet: gcc version 4.1.2 20060613 (prerelease) (Debian 4.1.1-5)


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


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

end of thread, other threads:[~2008-07-04 16:17 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-12-14  6:29 [Bug c++/34459] New: incorrect code when compiled with optimization (-O1) noblige at gmail dot com
2007-12-14 13:32 ` [Bug c++/34459] [4.1/4.3 Regression] " rguenth at gcc dot gnu dot org
2007-12-14 13:42 ` rguenth at gcc dot gnu dot org
2007-12-17 15:08 ` jakub at gcc dot gnu dot org
2007-12-19 15:32 ` jakub at gcc dot gnu dot org
2007-12-19 17:04 ` jakub at gcc dot gnu dot org
2007-12-19 20:50 ` jakub at gcc dot gnu dot org
2007-12-20  9:32 ` jakub at gcc dot gnu dot org
2007-12-20 14:41 ` jakub at gcc dot gnu dot org
2007-12-20 14:43 ` [Bug c++/34459] [4.1 " jakub at gcc dot gnu dot org
2008-07-04 16:17 ` jsm28 at gcc dot gnu dot 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).